编程语言
首页 > 编程语言> > Python处理csv文件

Python处理csv文件

作者:互联网

Python处理csv文件

1. 逐行写入csv

import csv

subject = ["语文","数学","英语","生物"]
score_first = [75, 90, 70, 77]
score_second = [80, 90, 80, 80]
score_third = [85, 90, 75, 88]
score_fourth = [90, 90, 80, 90]
with open ("test.csv", 'w', encoding="utf8", newline="") as f:
    # 基于文件对象构建csv写入对象
    csv_write = csv.writer(f)
    # 逐行写入
    csv_write.writerow(subject)
    csv_write.writerow (score_first)
    csv_write.writerow (score_second)
    csv_write.writerow (score_third)
    csv_write.writerow (score_fourth)

2. 逐行读取csv

with open ("test.csv", 'r', encoding="utf8", newline="") as f:
    # 基于文件对象构建读取对象
    csv_read = csv.reader(f)
    # 逐行读取
    for row in csv_read:
        print (row)

标签:文件,writerow,Python,write,score,90,csv,逐行
来源: https://www.cnblogs.com/caojun97/p/16244189.html