【发布时间】:2019-05-14 23:45:28
【问题描述】:
我正在编写一个 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."]
有趣的是,如果我复制 print 的输出并将其硬编码到 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 文件是正确的。我错过了什么?
【问题讨论】:
-
代码正确。您用来打开 csv 文件的工具是什么?请尝试在原版编辑器中打开它。
-
你是对的,在数字上文件看起来很乱,但在 Excel 上它看起来应该如此!谢谢!