0

I have a question about how to save a list as a CSV file in my way:

If I have a big list with many sub-lists: records = [['a','b','c','d'], ['1','2','3','4'], ......]

and i use this code to generate a csv file:

import csv
create = csv.writer(open(filename, "wb"))
create.writerow(records)

it displays all sub-list in one row and each one is in one cell: ['a','b','c','d'] ['1','2','3','4'].....

However i don't want the square brackets and quotation marks, but want every item in one cell, and only one sub-list per row: a,b,c,d 1,2,3,4

I'm new to python and really know few about the codes. hope u can help me :)

1
  • 1
    Have you tried to loop on your data and call writerow() for each of them ? Commented Aug 27, 2012 at 9:41

3 Answers 3

2
with open(filename, "wb") as file:
    csv.writer(file).writerows(records)
Sign up to request clarification or add additional context in comments.

Comments

1
import csv
create = csv.writer(open(filename, "wb"))
for element in record:
    create.writerow(element)

Should give you what you want.

Comments

0

You need to iterate over the records structure. Something like this:

for x in records:
    create.writerow(x)

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.