3 回答
TA贡献1859条经验 获得超6个赞
import csv
with open(..., 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
编辑:这仅适用于python2.x。
为了使其与python 3.x wb一起工作,请替换为w(请参阅此SO答案)
with open(..., 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
TA贡献1757条经验 获得超8个赞
这是Alex Martelli的安全版本:
import csv
with open('filename', 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
TA贡献1794条经验 获得超8个赞
使用python的csv模块读取和写入逗号或制表符分隔的文件。首选csv模块,因为它可以使您更好地控制报价。
例如,这是为您准备的示例:
import csv
data = ["value %d" % i for i in range(1,4)]
out = csv.writer(open("myfile.csv","w"), delimiter=',',quoting=csv.QUOTE_ALL)
out.writerow(data)
产生:
"value 1","value 2","value 3"
添加回答
举报