Write a list of lists into csv in Python [duplicat

2019-09-22 03:15发布

This question already has an answer here:

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

2条回答
祖国的老花朵
2楼-- · 2019-09-22 03:44

Use:

import csv
with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(list(zip(*l)))
查看更多
Rolldiameter
3楼-- · 2019-09-22 03:59

Try this:

import csv
with open('output.csv', 'w', newline='') as f:
    writer = csv.writer(f, delimiter=' ')
    writer.writerows(a)
查看更多
登录 后发表回答