我有一长串以下表格列表---
a = [[1.2,'abc',3],[1.2,'werew',4],........,[1.4,'qew',2]]
即。列表中的值是不同的类型 - 浮点数,整数,字符串。如何将其写入csv文件,以便我的输出csv文件看起来像
1.2,abc,3
1.2,werew,4
.
.
.
1.4,qew,2
答案 0 :(得分:252)
Python内置的CSV module可以轻松处理这个问题:
import csv
with open("output.csv", "wb") as f:
writer = csv.writer(f)
writer.writerows(a)
这假设您的列表定义为a
,就像您的问题一样。您可以通过各种可选参数将输出CSV的确切格式调整为csv.writer()
,如上面链接的库参考页中所述。
答案 1 :(得分:26)
import csv
with open(file_path, 'a') as outcsv:
#configure writer to write standard csv file
writer = csv.writer(outcsv, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
writer.writerow(['number', 'text', 'number'])
for item in list:
#Write item to outcsv
writer.writerow([item[0], item[1], item[2]])
答案 2 :(得分:25)
您可以使用pandas
:
In [1]: import pandas as pd
In [2]: a = [[1.2,'abc',3],[1.2,'werew',4],[1.4,'qew',2]]
In [3]: my_df = pd.DataFrame(a)
In [4]: my_df.to_csv('my_csv.csv', index=False, header=False)
答案 3 :(得分:8)
在我非常大的列表中使用csv.writer花了很长时间。我决定使用熊猫,它更快,更容易控制和理解:
import pandas
yourlist = [[...],...,[...]]
pd = pandas.DataFrame(yourlist)
pd.to_csv("mylist.csv")
你可以改变一些事情以制作更好的csv文件:
yourlist = [[...],...,[...]]
columns = ["abcd","bcde","cdef"] #a csv with 3 columns
index = [i[0] for i in yourlist] #first element of every list in yourlist
not_index_list = [i[1:] for i in yourlist]
pd = pandas.DataFrame(not_index_list, columns = columns, index = index)
#Now you have a csv with columns and index:
pd.to_csv("mylist.csv")
答案 4 :(得分:7)
如果出于某种原因您想手动执行此操作(不使用csv
,pandas
,numpy
等模块):
with open('myfile.csv','w') as f:
for sublist in mylist:
for item in sublist:
f.write(item + ',')
f.write('\n')
当然,滚动自己的版本可能容易出错并且效率低下......这通常就是为什么有一个模块。但有时候写自己的东西可以帮助你理解它们是如何工作的,有时它会更容易。
答案 5 :(得分:3)
Ambers的解决方案也适用于numpy数组:
from pylab import *
import csv
array_=arange(0,10,1)
list_=[array_,array_*2,array_*3]
with open("output.csv", "wb") as f:
writer = csv.writer(f)
writer.writerows(list_)
答案 6 :(得分:1)
确保在创建作者时指出lineterinator='\n'
;否则,当数据源来自其他csv文件时,可能会在每个数据行之后将额外的空行写入文件...
这是我的解决方案:
with open('csvfile', 'a') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
for i in range(0, len(data)):
spamwriter.writerow(data[i])
答案 7 :(得分:1)
如何将列表列表转储到pickle中并用pickle module进行恢复?很方便
>>> import pickle
>>>
>>> mylist = [1, 'foo', 'bar', {1, 2, 3}, [ [1,4,2,6], [3,6,0,10]]]
>>> with open('mylist', 'wb') as f:
... pickle.dump(mylist, f)
>>> with open('mylist', 'rb') as f:
... mylist = pickle.load(f)
>>> mylist
[1, 'foo', 'bar', {1, 2, 3}, [[1, 4, 2, 6], [3, 6, 0, 10]]]
>>>
答案 8 :(得分:0)
如果您不想为此导入var log = console.log;
var console = {};
console.log = log;
log('foo');
模块,则可以仅使用Python内置插件将列表列表写入csv文件中。
<Alter ObjectExpansion="ExpandFull" xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
<Object>
<DatabaseID>YourOlapDbNameHere</DatabaseID>
</Object>
<ObjectDefinition>
<Database xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ddl2="http://schemas.microsoft.com/analysisservices/2003/engine/2" xmlns:ddl2_2="http://schemas.microsoft.com/analysisservices/2003/engine/2/2" xmlns:ddl100_100="http://schemas.microsoft.com/analysisservices/2008/engine/100/100" xmlns:ddl200="http://schemas.microsoft.com/analysisservices/2010/engine/200" xmlns:ddl200_200="http://schemas.microsoft.com/analysisservices/2010/engine/200/200" xmlns:ddl300="http://schemas.microsoft.com/analysisservices/2011/engine/300" xmlns:ddl300_300="http://schemas.microsoft.com/analysisservices/2011/engine/300/300" xmlns:ddl400="http://schemas.microsoft.com/analysisservices/2012/engine/400" xmlns:ddl400_400="http://schemas.microsoft.com/analysisservices/2012/engine/400/400" xmlns:ddl500="http://schemas.microsoft.com/analysisservices/2013/engine/500" xmlns:ddl500_500="http://schemas.microsoft.com/analysisservices/2013/engine/500/500">
<ID>YourOlapDbNameHere</ID>
<Name>YourOlapDbNameHere</Name>
<ddl200:CompatibilityLevel>1100</ddl200:CompatibilityLevel>
<Language>1033</Language>
<Collation>Latin1_General_CI_AS</Collation>
<DataSourceImpersonationInfo>
<ImpersonationMode>Default</ImpersonationMode>
答案 9 :(得分:0)
在csv.writer函数中使用 newline 参数跟踪示例时,出现错误消息。以下代码对我有用。
with open(strFileName, "w") as f:
writer = csv.writer(f, delimiter=',', quoting=csv.QUOTE_MINIMAL)
writer.writerows(result)
答案 10 :(得分:0)
如果将 lists 的 lists 的 lll
list 导出到 .csv,这将适用于 Python3: >
import csv
with open("output.csv", "w") as f:
writer = csv.writer(f)
for element in lll:
writer.writerows(element)