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
我正在尝试使用Python列表中的值创建.csv文件。当我打印列表中的值时,它们都是unicode(?),即它们看起来像这样
[u'value 1', u'value 2', ...]
If I iterate through the values in the list i.e. for v in mylist: print v
they appear to be plain text.
如果我遍历列表中的值,即m in mylist中的值:print v它们看起来是纯文本。
And I can put a ,
between each with print ','.join(mylist)
我可以在每个之间加上'',''。join(mylist)
And I can output to a file, i.e.
我可以输出到一个文件,即
myfile = open(...)
print >>myfile, ','.join(mylist)
But I want to output to a CSV and have delimiters around the values in the list e.g.
但是我想输出到CSV并且在列表中的值周围有分隔符,例如
"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?
我找不到一种简单的方法来在格式中包含分隔符,例如我试过了join语句。我怎样才能做到这一点?
10 个解决方案
#1
159
import csv
with open(..., 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
#2
77
Here is a secure version of Alex Martelli's:
这是Alex Martelli的安全版本:
import csv
with open('filename', 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
#3
12
Use python's csv
module for reading and writing comma or tab-delimited files. The csv module is preferred because it gives you good control over quoting.
使用python的csv模块读取和写入逗号或制表符分隔的文件。 csv模块是首选,因为它可以很好地控制引用。
For example, here is the worked example for you:
例如,以下是您的工作示例:
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)
Produces:
生产:
"value 1","value 2","value 3"
#4
12
The best option I've found was using the savetxt
from the numpy
module:
我发现的最佳选择是使用numpy模块中的savetxt:
import numpy as np
np.savetxt("file_name.csv", data1, delimiter=",", fmt='%s', header=header)
In case you have multiple lists that need to be stacked
如果您有多个需要堆叠的列表
np.savetxt("file_name.csv", np.column_stack((data1, data2)), delimiter=",", fmt='%s', header=header)
#5
10
For another approach, you can use DataFrame in pandas: And it can easily dump the data to csv just like the code below:
对于另一种方法,您可以在pandas中使用DataFrame:它可以轻松地将数据转储到csv,就像下面的代码一样:
import pandas
df = pandas.DataFrame(data={"col1": list_1, "col2": list_2})
df.to_csv("./file.csv", sep=',',index=False)
#6
6
You could use the string.join method in this case.
在这种情况下,您可以使用string.join方法。
Split over a few of lines for clarity - here's an interactive session
为清晰起见,分成几行 - 这是一个互动会话
>>> a = ['a','b','c']
>>> first = '", "'.join(a)
>>> second = '"%s"' % first
>>> print second
"a", "b", "c"
Or as a single line
或者作为一条线
>>> print ('"%s"') % '", "'.join(a)
"a", "b", "c"
However, you may have a problem is your strings have got embedded quotes. If this is the case you'll need to decide how to escape them.
但是,您可能遇到的问题是您的字符串已嵌入引号。如果是这种情况,您需要决定如何逃避它们。
The CSV module can take care of all of this for you, allowing you to choose between various quoting options (all fields, only fields with quotes and seperators, only non numeric fields, etc) and how to esacpe control charecters (double quotes, or escaped strings). If your values are simple, string.join will probably be OK but if you're having to manage lots of edge cases, use the module available.
CSV模块可以为您处理所有这些,允许您在各种引用选项(所有字段,仅包含引号和分隔符的字段,仅非数字字段等)之间进行选择以及如何控制字符串(双引号,或者逃脱的字符串)。如果您的值很简单,string.join可能会正常,但如果您需要管理大量边缘情况,请使用可用模块。
#7
1
you should use the CSV module for sure , but the chances are , you need to write unicode . For those Who need to write unicode , this is the class from example page , that you can use as a util module:
你应该确定使用CSV模块,但是你可能需要编写unicode。对于那些需要编写unicode的人来说,这是示例页面中的类,您可以将其用作util模块:
import csv, codecs, cStringIO
class UTF8Recoder:
"""
Iterator that reads an encoded stream and reencodes the input to UTF-8
"""
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
return self.reader.next().encode("utf-8")
class UnicodeReader:
"""
A CSV reader which will iterate over lines in the CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
def next(self):
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self
class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
self.writer.writerow([s.encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
#8
1
Here is another solution that does not require the csv
module.
这是另一个不需要csv模块的解决方案。
print ', '.join(['"'+i+'"' for i in myList])
Example :
示例:
>>> myList = [u'value 1', u'value 2', u'value 3']
>>> print ', '.join(['"'+i+'"' for i in myList])
"value 1", "value 2", "value 3"
However, if the initial list contains some ", they will not be escaped. If it is required, it is possible to call a function to escape it like that :
但是,如果初始列表包含一些“,它们将不会被转义。如果需要,可以调用一个函数来逃避它:
print ', '.join(['"'+myFunction(i)+'"' for i in myList])
#9
0
import csv
with open("file.csv", 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
#10
0
Jupyter notebook
Lets say that your list is A
让我们说你的清单是A.
Then you can code the following ad you will have it as a csv file (columns only!)
然后,您可以将以下广告编码为csv文件(仅限列!)
R="\n".join(A)
f = open('Columns.csv','w')
f.write(R)
f.close()
#1
159
import csv
with open(..., 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
#2
77
Here is a secure version of Alex Martelli's:
这是Alex Martelli的安全版本:
import csv
with open('filename', 'wb') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
#3
12
Use python's csv
module for reading and writing comma or tab-delimited files. The csv module is preferred because it gives you good control over quoting.
使用python的csv模块读取和写入逗号或制表符分隔的文件。 csv模块是首选,因为它可以很好地控制引用。
For example, here is the worked example for you:
例如,以下是您的工作示例:
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)
Produces:
生产:
"value 1","value 2","value 3"
#4
12
The best option I've found was using the savetxt
from the numpy
module:
我发现的最佳选择是使用numpy模块中的savetxt:
import numpy as np
np.savetxt("file_name.csv", data1, delimiter=",", fmt='%s', header=header)
In case you have multiple lists that need to be stacked
如果您有多个需要堆叠的列表
np.savetxt("file_name.csv", np.column_stack((data1, data2)), delimiter=",", fmt='%s', header=header)
#5
10
For another approach, you can use DataFrame in pandas: And it can easily dump the data to csv just like the code below:
对于另一种方法,您可以在pandas中使用DataFrame:它可以轻松地将数据转储到csv,就像下面的代码一样:
import pandas
df = pandas.DataFrame(data={"col1": list_1, "col2": list_2})
df.to_csv("./file.csv", sep=',',index=False)
#6
6
You could use the string.join method in this case.
在这种情况下,您可以使用string.join方法。
Split over a few of lines for clarity - here's an interactive session
为清晰起见,分成几行 - 这是一个互动会话
>>> a = ['a','b','c']
>>> first = '", "'.join(a)
>>> second = '"%s"' % first
>>> print second
"a", "b", "c"
Or as a single line
或者作为一条线
>>> print ('"%s"') % '", "'.join(a)
"a", "b", "c"
However, you may have a problem is your strings have got embedded quotes. If this is the case you'll need to decide how to escape them.
但是,您可能遇到的问题是您的字符串已嵌入引号。如果是这种情况,您需要决定如何逃避它们。
The CSV module can take care of all of this for you, allowing you to choose between various quoting options (all fields, only fields with quotes and seperators, only non numeric fields, etc) and how to esacpe control charecters (double quotes, or escaped strings). If your values are simple, string.join will probably be OK but if you're having to manage lots of edge cases, use the module available.
CSV模块可以为您处理所有这些,允许您在各种引用选项(所有字段,仅包含引号和分隔符的字段,仅非数字字段等)之间进行选择以及如何控制字符串(双引号,或者逃脱的字符串)。如果您的值很简单,string.join可能会正常,但如果您需要管理大量边缘情况,请使用可用模块。
#7
1
you should use the CSV module for sure , but the chances are , you need to write unicode . For those Who need to write unicode , this is the class from example page , that you can use as a util module:
你应该确定使用CSV模块,但是你可能需要编写unicode。对于那些需要编写unicode的人来说,这是示例页面中的类,您可以将其用作util模块:
import csv, codecs, cStringIO
class UTF8Recoder:
"""
Iterator that reads an encoded stream and reencodes the input to UTF-8
"""
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
return self.reader.next().encode("utf-8")
class UnicodeReader:
"""
A CSV reader which will iterate over lines in the CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
def next(self):
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self
class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
self.writer.writerow([s.encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
#8
1
Here is another solution that does not require the csv
module.
这是另一个不需要csv模块的解决方案。
print ', '.join(['"'+i+'"' for i in myList])
Example :
示例:
>>> myList = [u'value 1', u'value 2', u'value 3']
>>> print ', '.join(['"'+i+'"' for i in myList])
"value 1", "value 2", "value 3"
However, if the initial list contains some ", they will not be escaped. If it is required, it is possible to call a function to escape it like that :
但是,如果初始列表包含一些“,它们将不会被转义。如果需要,可以调用一个函数来逃避它:
print ', '.join(['"'+myFunction(i)+'"' for i in myList])
#9
0
import csv
with open("file.csv", 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(mylist)
#10
0
Jupyter notebook
Lets say that your list is A
让我们说你的清单是A.
Then you can code the following ad you will have it as a csv file (columns only!)
然后,您可以将以下广告编码为csv文件(仅限列!)
R="\n".join(A)
f = open('Columns.csv','w')
f.write(R)
f.close()