python读写csv文件

读取csv文件

代码

import csv
with open('总分.csv', 'r', encoding='gbk', newline='') as f:
    # 读取文件所有信息
    LIST = csv.reader(f)
    content2 = list(LIST)
    for i in LIST:
        f.close()
print(content2)

运行截图

写csv文件

代码

import csv
with open('history.csv', "w", encoding='gbk', newline="") as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(["类型", "时间", "金额", "备注"])
    csv_writer.writerow(["类型1", "时间2", "金额3", "备注4"])

截图

posted @ 2021-01-01 16:04  萌新_python  阅读(96)  评论(0编辑  收藏  举报