写入内容:
class csv.DictWriter(f,fieldnames,restval ='',extrasaction ='raise',dialect ='excel',* args,** kwds )
import csv
with open('names.csv', 'w', newline='') as csvfile:
#设置需要写入的属性
fieldnames = ['first_name', 'last_name']
#创建DicWriter类,实例化后可写入或读取
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
#创建实例化写入对象
writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})