像这样。

解决python写入csv文件每两行间 隔一个空行的问题

这是因为导入的时候,是这样写的:

with open('book_statics.csv', 'w+') as csvfile:
    writer = csv.writer(csvfile)
    for i in ans:
        writer.writerow(i)

只要改成wb+,也就是写二进制文件,就好了吗?

with open('book_statics.csv', 'wb+') as csvfile:
    writer = csv.writer(csvfile)
    for i in ans:
        writer.writerow(i)

 并没有!!!

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

查阅了官方文档,改成如下即可

with open('book_statics.csv', 'w+', newline='') as csvfile:
    writer = csv.writer(csvfile)
    for i in ans:
        writer.writerow(i)

  

成功解决。

相关文章:

  • 2021-10-23
  • 2022-01-22
  • 2022-12-23
  • 2021-08-15
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2021-03-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
相关资源
相似解决方案