0

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)  
6
  • Possible duplicate of Python export csv data into file Commented Aug 16, 2016 at 15:30
  • Where is strList defined? Also, sample data would help. Commented Aug 16, 2016 at 15:30
  • What is strList? It should be a collection of rows (['r1', 'r1c2'], ['r2', 'r2c2']]). Commented Aug 16, 2016 at 15:31
  • yes it is a collection of rows printing data like this ['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'] Commented Aug 16, 2016 at 15:36
  • @Jsant: That's what you think it is, but the problem you're having indicates otherwise. Look at the output of print strList and you will probably find it's not a list of lists. Commented Aug 16, 2016 at 15:38

1 Answer 1

0

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

hi,yes
hi,ast
ever,as
no,yes
redi,google
redi,asdf
redi,asd
erfa,asdf
hi,dsa
(EOF):
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.