Skip to content Skip to sidebar Skip to footer

Write A List Of Lists Into Csv In Python

I have a list of lists of pure data like this a=[[1,2,3], [4,5,6], [7,8,9]] How can I write a into a CSV file with each list in a column like this? 1 4 7 2 5 8 3 6 9

Solution 1:

Use:

import csv
withopen('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(list(zip(*l)))

Solution 2:

Try this:

import csv
withopen('output.csv', 'w', newline='') as f:
    writer = csv.writer(f, delimiter=' ')
    writer.writerows(a)

Post a Comment for "Write A List Of Lists Into Csv In Python"