解決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)

  

成功解決。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM