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']
posted @ 2022-09-16 23:46  筱团  阅读(34)  评论(0编辑  收藏  举报