我试图用Python列表中的值创建一个.csv文件。当我打印列表中的值时,它们都是unicode(?),即它们看起来像这样

[u'value 1', u'value 2', ...]

如果我遍历列表中的值,即mylist中的v:打印v,它们看起来是纯文本。

然后在每一个之间加上print '。join(mylist)

我可以输出到一个文件,即。

myfile = open(...)
print >>myfile, ','.join(mylist)

但是我想输出到CSV,并在列表中的值周围有分隔符。

"value 1", "value 2", ... 

我找不到一个简单的方法来包括在格式分隔符,例如,我已经尝试通过连接语句。我该怎么做呢?


当前回答

创建并写入csv文件

下面的示例演示创建和写入csv文件。 要使一个动态文件写入器,我们需要导入一个包import csv,然后需要创建一个文件引用的文件实例 Ex:-使用open("D:\sample.csv","w",newline="")作为文件写入器

here if the file does not exist with the mentioned file directory then python will create a same file in the specified directory, and "w" represents write, if you want to read a file then replace "w" with "r" or to append to existing file then "a". newline="" specifies that it removes an extra empty row for every time you create row so to eliminate empty row we use newline="", create some field names(column names) using list like fields=["Names","Age","Class"], then apply to writer instance like writer=csv.DictWriter(file_writer,fieldnames=fields) here using Dictionary writer and assigning column names, to write column names to csv we use writer.writeheader() and to write values we use writer.writerow({"Names":"John","Age":20,"Class":"12A"}) ,while writing file values must be passed using dictionary method , here the key is column name and value is your respective key value

import csv 

with open("D:\\sample.csv","w",newline="") as file_writer:

   fields=["Names","Age","Class"]

   writer=csv.DictWriter(file_writer,fieldnames=fields)

   writer.writeheader()

   writer.writerow({"Names":"John","Age":21,"Class":"12A"})

其他回答

对于那些寻找简单解决方案的人。实际上,我发现了一个更简单的解决方案,可以完成类似的工作:

import pandas as pd
a = ['a','b','c'] 
df = pd.DataFrame({'a': a})
df= df.set_index('a').T
df.to_csv('list_a.csv', index=False)

希望这也能有所帮助。

使用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"

下面是另一个不需要csv模块的解决方案。

print ', '.join(['"'+i+'"' for i in myList])

例子:

>>> myList = [u'value 1', u'value 2', u'value 3']
>>> print ', '.join(['"'+i+'"' for i in myList])
"value 1", "value 2", "value 3"

但是,如果初始列表包含一些“,则不会转义。如果需要,可以调用一个函数像这样转义它:

print ', '.join(['"'+myFunction(i)+'"' for i in myList])

创建并写入csv文件

下面的示例演示创建和写入csv文件。 要使一个动态文件写入器,我们需要导入一个包import csv,然后需要创建一个文件引用的文件实例 Ex:-使用open("D:\sample.csv","w",newline="")作为文件写入器

here if the file does not exist with the mentioned file directory then python will create a same file in the specified directory, and "w" represents write, if you want to read a file then replace "w" with "r" or to append to existing file then "a". newline="" specifies that it removes an extra empty row for every time you create row so to eliminate empty row we use newline="", create some field names(column names) using list like fields=["Names","Age","Class"], then apply to writer instance like writer=csv.DictWriter(file_writer,fieldnames=fields) here using Dictionary writer and assigning column names, to write column names to csv we use writer.writeheader() and to write values we use writer.writerow({"Names":"John","Age":20,"Class":"12A"}) ,while writing file values must be passed using dictionary method , here the key is column name and value is your respective key value

import csv 

with open("D:\\sample.csv","w",newline="") as file_writer:

   fields=["Names","Age","Class"]

   writer=csv.DictWriter(file_writer,fieldnames=fields)

   writer.writeheader()

   writer.writerow({"Names":"John","Age":21,"Class":"12A"})

这个解决方案听起来很疯狂,但效果非常好

import csv

with open('filename', 'wb') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL,delimiter='\n')
    wr.writerow(mylist)

文件是由csvwriter写入的,因此csv属性被维护,即逗号分隔。 分隔符通过每次将列表项移动到下一行来帮助主要部分。