csv.writer写入文件有多余的空行

在用csv.writer写入文件的时候发现中间有多余的空行。

最早打开方式只是‘w’,会出现多余的空行,网上建议使用binary形式‘wb’打开可以解决问题:

with open('egg2.csv', 'wb') as cf:
  • 1
  • 2

不过只能在python2下运行,python3报错:

TypeError: a bytes-like object is required, not 'str'

应该是:

加上newline=' ',并不了解为什么。

with open(csv_file_name, 'w', newline='') as csv_file:

猜你喜欢

转载自blog.csdn.net/weixin_39241397/article/details/80597228
今日推荐