python存入csv
程序员文章站
2024-01-20 20:37:22
如题,后续继续优化 ......
如题,后续继续优化
import csv
# 构建表头
headers = ["id", "user_name", "age", "country"]
# 内容列表
rows = [
("001", "dana", 18, "china"),
("002", "tom", 22, "arimecan"),
("003", "jack", 45, "hk")
]
# 新建csv文档,默认是自动换行的,所以要 newline=""
with open("csv01.csv", "w", newline='') as f:
f_csv=csv.writer(f)
f_csv.writerow(headers)
# 由于 rows是集合,需要遍历写入每一行,不然就堆在单元格
for row in rows:
f_csv.writerow(row)
print("finsh")
上一篇: 判断file框选择的是否为图片
下一篇: Mysql 索引