python内置了csv模块,无需第三方依赖包,直接生成csv表格文件。
import csv
# 表头
field_order = ["姓名", '年龄', '性别']
with open("test.csv", 'w', encoding="utf-8", newline='') as csvfile:
writer = csv.DictWriter(csvfile, field_order)
writer.writeheader()
writer.writerow(dict(zip(field_order, ["张三", 20, "男"])))
writer.writerow(dict(zip(field_order, ["李四", 10, "男"])))
writer.writerow(dict(zip(field_order, ["王五", 30, "男"])))
关注公众号「Python之禅」,回复「1024」免费获取Python资源