import csv
rowlist=[{‘first_name‘: ‘mark‘, ‘last_name‘: ‘zhao‘,‘age‘:21},
{‘first_name‘: ‘tony‘, ‘last_name‘: ‘wang‘,‘age‘:22},
{‘first_name‘: ‘hengry‘, ‘last_name‘: ‘zhang‘,‘age‘:23},]
#newline设置为空
with open(‘names.csv‘, ‘w‘,newline=‘‘) as csvfile:
#构造三个列
col_names = [‘first_name‘, ‘last_name‘,‘age‘]
writer = csv.DictWriter(csvfile, col_names)
#从头部开始按行写入数据
writer.writeheader()
writer.writerows(rowlist)
print(".......................writer over............")
时间: 2024-10-22 02:02:29