python存入csv

2022-10-17,,

如题,后续继续优化

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")

  

 

《python存入csv.doc》

下载本文的Word格式文档,以方便收藏与打印。