CSV文件存储
CSV,(Comma-Separated Values)其文件以纯文本形式存储表格数据。
写入
import csv
with open('data.csv', 'w') as f: #打开data.csv文件
writer = csv.writer(f, delimiter=',')#分隔符为逗号
writer.writerow(['ID','NAME','AGE']) #writerow()可以写入一行数据
writer.writerow(['001','Tashi',20])
writer.writerow(['002', 'Aha', 18])
writer.writerow(['003', 'Yile', 16])
说明
- writerow()写入一行数据
- writerows()写入多行数据
读取
import csv
with open('data.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)