writer = csv.writer(f, doublequote=False, escapechar=‘@‘)
writer参数为一个可写入的文件对象,并返回一个Dictwriter实例,实例化出的对象有writerow与writerows
Dictwriter.writerow(iterable) 单行写入
Dictwriter.writerows(iterable) 多行迭代写入
在windows系统下打开文件对象时,需要指定newline为空,否则结束符被‘\n’转换
file = ‘file1.csv‘
with open(file,‘w‘,encoding=‘utf-8‘,newline=‘‘) as f:
w_content = csv.writer(f)
w_content.writerow([‘id‘,‘name‘,"age""s"])
w_content.writerows([(1,‘tom‘,12),{2,‘nani‘,13}])
csv.reader读一个可读的文件对象,返回一个行迭代器
with open(file,encoding=‘utf-8‘,newline=‘‘) as f:
r_content = csv.reader(f)
for i in r_content:
print(i)
原文:https://www.cnblogs.com/zoer/p/13311214.html