将数据导出到csv python

时间:2016-08-16 15:24:56

标签: python csv export-to-csv

当我使用下面的代码时,我的数据会被导出但是它的所有内容都会一直向下,有什么帮助吗?

b = open('tester.csv', 'wb')
a = csv.writer(b)
while (count < x):
    tags = str(data['alerts'][count]  ['tags']).replace("u\"","\"").replace("u\'","\'")
a.writerows(strList)  

1 个答案:

答案 0 :(得分:0)

鉴于您的数据,我没有看到这可能会出错,除非您的数据实际上看起来不像这样。

>>> data = [['hi', 'yes', 'bye', 'def'], ['hi', 'ast', 'cnx', 'vplex'], ['ever', 'as', 'no', 'qwerty', 'redi'], ['no', 'yes', 'qwerty'], ['redi', 'google'], ['redi', 'asdf', 'asdfef', 'wer'], ['redi', 'asd', 'rrr', 'www', 'qqq'], ['erfa', 'asdf', 'fef'], ['hi', 'dsa', 'f3e']]
>>> with open('my.csv','w') as f:
...  for item in data:
...   my_item = item[0]+','+item[1]+'\n'
...   f.write(my_item)
... 

pg my.csv

hi,yes
hi,ast
ever,as
no,yes
redi,google
redi,asdf
redi,asd
erfa,asdf
hi,dsa
(EOF):