python 读写 CSV


写入 CSV

1. 写入多行(writerows

import csv

with open('test.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(["6","6","6"])

test.csv 内容如下:

6
6
6

2. 写入一行(writerow

import csv

with open('test.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(["6","6","6"])

test.csv 内容如下:

6,6,6

读取 CSV

import csv

with open('test.csv', newline='', encoding='utf-8') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

# ['6', '6', '6']

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289904.html

(0)
上一篇 2022年9月17日
下一篇 2022年9月17日

相关推荐

发表回复

登录后才能评论