I have a list of lists example: list_of_lists = [['name1', 'number1', 'comment1'], ['name2', 'number2', 'comment2'], ['name3', 'number3', 'comment3']]
I want to write them into a csv file that should look like this:
NAME NUMBER COMMENT
name1 number1 comment1
name2 number2 comment2
name3 number3 comment3
I tried:
rows = zip(*list_of_lists)
import csv
with open('new.csv', "w") as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
But that writes it as:
name1 name2 name3
number1 number2 comment2
comment1 number3 comment3
If I use rows = zip(list_of_lists) instead of rows = zip(*list_of_lists), I get all data as string in csv:
"name1, number1, comment1"
"name2, number2, comment2"
"name3, number3, comment3"
How do I get the desired result?
zip()at all ? Your data are already in the right format.