[英]Python: appending a row in a csv file cause it to be split randomly
我正在編寫一個python(3.7)函數,它接受一個字符串並將其附加到.csv文件中。 這是代碼:
def write_to_file(row):
print("row: ",[row])
with open('output.csv', mode='a') as output_file:
output_file_writer = csv.writer(output_file)
output_file_writer.writerow([row])
當我打印行時,我得到的結果如下:
row: ["Introducing company, the tool that let's you manage your coins alt-coin on coinbase-pro, and many other exchanges automatically."]
有趣的是,如果我復制打印輸出並在write
命令中對其進行硬編碼,如下所示:
output_file_writer.writerow(["Introducing company, the tool that let's you manage your coins alt-coin on coinbase-pro, and many other exchanges automatically."])
.csv文件是正確的。 我錯過了什么?
嘗試在open()中使用newline ='',即with open('output.csv', mode='a', newline='') as output_file:
引自python docs :
如果未指定newline ='',則不會正確解釋嵌入在引用字段中的換行符,並且在寫入時使用\\ r \\ n linendings的平台上將添加額外的\\ r \\ n。 指定newline =''應始終是安全的,因為csv模塊執行自己的(通用)換行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.