使用Python列表中的值创建.csv文件 [英] Create a .csv file with values from a Python list
本文介绍了使用Python列表中的值创建.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用Python列表中的值创建.csv文件。当我打印列表中的值时,它们都是unicode(?),即它们看起来像这样
I am trying to create a .csv file with the values from a Python list. When I print the values in the list they are all unicode (?), i.e. they look something like this
[u'value 1', u'value 2', ...]
如果我遍历 c>
我可以把一个,之间
print','。join(mylist)
我可以输出到一个文件,例如
And I can output to a file, i.e.
myfile = open(...)
print >>myfile, ','.join(mylist)
但我想输出为CSV并在列表中的值周围有分隔符,例如
But I want to output to a CSV and have delimiters around the values in the list e.g.
"value 1", "value 2", ...
我找不到一个简单的方法来在格式化中包括分隔符,例如我已经尝试通过加入
语句。我该如何做?
I can't find an easy way to include the delimiters in the formatting, e.g. I have tried through the join
statement. How can I do this?
推荐答案
import csv
myfile = open(..., 'wb')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
这篇关于使用Python列表中的值创建.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文