这是读入
1 import csv 2 #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() 3 with open("XXX.csv","r",encoding="utf-8") as csvfile: 4 #读取csv文件,返回的是迭代类型 5 read = csv.reader(csvfile) 6 for i in read: 7 print(i)
写出
1 import csv 2 with open("XXX.csv","w",newline="") as datacsv: 3 #dialect为打开csv文件的方式,默认是excel,delimiter=" "参数指写入的时候的分隔符 4 csvwriter = csv.writer(datacsv,dialect = ("excel")) 5 #csv文件插入一行数据,把下面列表中的每一项放入一个单元格(可以用循环插入多行) 6 csvwriter.writerow(["A","B","C","D"])
转载自:http://www.cnblogs.com/zle1992/p/5986317.html