csv写入:
import csv
# 文件写入
with open('data.csv', 'w') as csvfile:
# 初始化写入对象
f_csv_wt = csv.writer(csvfile)
# writerow 写入
f_csv_wt.writerow(['id', 'name', 'age'])
# writerows 写入多行
f_csv_wt.writerows(['id', 'name', 'age'],[0, 1, 2])
# 写入的内容会在一列以空格分开
# f_csv_wt = csv.writer(csvfile,delimiter=' ')
# 写入字典格式数据
filetags = ['id', 'name', 'age']
writer = csv.DictWriter(csvfile, fieldnames=filetags)
writer.writeheader() # 定义文件title头信息
f_csv_wt.writerow({'id': '0', 'name': '1', 'age': 2})
# 文件读取
with open('data.csv', 'w') as csvfile:
# 初始化读取对象
f_csv_rd = csv.reader(csvfile)
#跳过title首行读取
header = next(f_csv_rd)