如何将两个列表中的数据写入csv中的列? [英] How to write data from two lists into columns in a csv?

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

问题描述

我想写数据,我必须创建一个CSV文件的直方图。我有我的箱子名单,我有我的频率列表。有人可以给我一些帮助,把他们写入一个csv在他们各自的列?

即第一列的频率和第二列的频率

解决方案

这个例子使用 izip (而不是 zip )来避免创建一个新列表并且不得不将它保存在内存中。它还使用 Python内置的csv模块,确保正确转义。作为一个额外的好处,它也避免使用任何循环,所以代码是简短的。

  import csv 
from itertools导入izip

与开放('some.csv','wb')作为f:
作家= csv.writer(f)
writer.writerows(izip ,频率))


I want to write data that I have to create a histogram into a csv file. I have my 'bins' list and I have my 'frequencies' list. Can someone give me some help to write them into a csv in their respective columns?

ie bins in the first column and frequency in the second column

解决方案

This example uses izip (instead of zip) to avoid creating a new list and having to keep it in the memory. It also makes use of Python's built in csv module, which ensures proper escaping. As an added bonus it also avoids using any loops, so the code is short and concise.

import csv
from itertools import izip

with open('some.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(izip(bins, frequencies))

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

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