Python寫入到csv文件存在空行的解決方法


我在使用Python將數據寫入到csv文件中,發現采用下面的方法,寫入到csv中會存在一行間一行的問題

with open(os.path.join(outpath,'result.csv'),'w') as cf:
        writer = csv.writer(cf)
        writer.writerow(['shader','file'])
        for key , value in result.items():
            writer.writerow([key,value])

為了解決這個問題,查了下資料,發現這是和打開方式有關,將打開的方法改為wb,就不存在這個問題了,也就是

在read/write csv 文件是要以binary的方式進行。

with open(os.path.join(outpath,'result.csv'),'wb') as cf:
        writer = csv.writer(cf)
        writer.writerow(['shader','file'])
        for key , value in result.items():
            writer.writerow([key,value])

 


免責聲明!

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



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