将Python列表写入csv中的列 [英] Writing Python lists to columns in csv

查看:4918
本文介绍了将Python列表写入csv中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个列表,长度相同,我想将它们写入CSV中的5列。到目前为止,我只能使用以下代码向列写入一个:

  with open('test.csv','wb ')as f:
writer = csv.writer(f)
for test_list中的val:
writer.writerow([val])
/ pre>

如果我为循环添加另一个,它只是将该列表写入同一列。

解决方案

将它们更改为行

  rows = zip(list1,list2,list3,list4,list5)

然后只是

 对于行中的行:
writer.writerow $ b


I have 5 lists, all of the same length, and I'd like to write them to 5 columns in a CSV. So far, I can only write one to a column with this code:

with open('test.csv', 'wb') as f:
    writer = csv.writer(f)
    for val in test_list:
        writer.writerow([val])

If I add another for loop, it just writes that list to the same column. Anyone know a good way to get five separate columns?

解决方案

change them to rows

rows = zip(list1,list2,list3,list4,list5)

then just

for row in rows:
    writer.writerow(row)

这篇关于将Python列表写入csv中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆