paopao00 2020-01-12
#coding=utf-8 import csv fp = open(‘C:/Users/93794/Desktop/test.csv‘,‘w+‘) writer = csv.writer(fp) writer.writerow((‘id‘,‘name‘)) writer.writerow((‘1‘,‘xiaoming‘)) writer.writerow((‘2‘,‘张三‘)) writer.writerow((‘3‘,‘李四‘))
以上代码会自动每一行多一空行
python3里面对 str和bytes类型做了严格的区分,不像python2里面某些函数里可以混用。所以用python3来写wirterow时,打开文件不要用wb模式,只需要使用w模式,然后带上newline=‘’
#coding=utf-8 import csv fp = open(‘C:/Users/93794/Desktop/test.csv‘,‘w+‘,newline=‘‘) writer = csv.writer(fp) writer.writerow((‘id‘,‘name‘)) writer.writerow((‘1‘,‘xiaoming‘)) writer.writerow((‘2‘,‘张三‘)) writer.writerow((‘3‘,‘李四‘))