CSV文件读写

CSV读取

源码

1
2
3
4
5
import csv

data = csv.reader(open('devin.csv', 'r'))
for i in data:
print(i)

执行结果

1
2
['devin', 'devin2', 'devin3']
['devin', 'devin2', 'devin3']

[!NOTE]

csv、reader

CSV写入

源码

1
2
3
4
5
6
7
import csv

stu = ["devin","devin2","devin3"]

out = open("devin.csv", 'a', newline="")
csv_writer = csv.writer(out, dialect='excel')
csv_writer.writerow(stu)

执行结果

devin.csv文件中追加了新的一行

[!NOTE]

list、open、newline、writer、dialect、writerow