GhostLWB 2020-03-03
如执行下面的代码:
import csv if __name__ == "__main__": content1 = [‘hello‘] content2 = [‘world‘] with open(‘test.csv‘, ‘w‘) as f: writer = csv.writer(f) writer.writerow(content1) writer.writerow(content2)
打开test.csv文件查看,如下图:
可以看到,多了一行空白行,解决方式如下:
如果使用的python3:
import csv if __name__ == "__main__": content1 = [‘hello‘] content2 = [‘world‘] with open(‘test.csv‘, ‘w‘, newline=‘‘, encoding=‘utf-8-sig‘) as f: writer = csv.writer(f) writer.writerow(content1) writer.writerow(content2)
如果使用的python2:
import csv if __name__ == "__main__": content1 = [‘hello‘] content2 = [‘world‘] with open(‘test.csv‘, ‘wb‘) as f: writer = csv.writer(f) writer.writerow(content1) writer.writerow(content2)