我有一个CSV文件,其中有
spam
在里面。然后,我做了
with open(directory, "a") as config_csv:
writer = csv.writer(config_csv)
writer.writerow(["something"])
writer.writerow(["something else"])
我期待
spam
something
something else
相反,我得到了
spam
"something"
"something else"
我如何得到我想要的东西?
答案 0 :(得分:2)
使用CSV模块,使用delimiter
,quotechar
和quoting=csv.QUOTE_MINIMAL
选项达到预期效果:
import csv
with open(file, "a", newline='') as config_csv:
writer = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(["something"])
writer.writerow(["something else"])
然后 file
将包含:
spam
something
something else
在Python 3.4上测试。
答案 1 :(得分:1)
SE中的其他答案对我不起作用。我所做的是:
with open('ADC.csv', 'a') as csvfile:
csv_Writer = csv.writer(csvfile, delimiter=';', lineterminator='\n')
答案 2 :(得分:0)
如果您需要的就是您所说的内容,则不需要模块csv
:
with open(directory, "a") as config_csv:
config_csv.write("something\n")
config_csv.write("something else\n")