简体   繁体   English

将数据导出到CSV python

[英]Exporting data to csv python

When I use the following code below, my data gets exported but its all in one column going all the way down, any help? 当我在下面使用以下代码时,我的数据将被导出,但所有数据都将一直列在下面,有什么帮助吗?

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)  

Given your data, I fail to see how this could go wrong, unless your data doesn't actually look like this. 给定您的数据,除非您的数据实际上看起来不是这样,否则我看不到这怎么可能出错。

>>> 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 pg my.csv

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM