将数组中的值输出到CSV文件。 [英] Outputting a Python dictionary with values in an array to a CSV file.

查看:199
本文介绍了将数组中的值输出到CSV文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题在这里,请原谅我确定的各种各样的错误。



我已经在这个工作了一段时间,我承认被扼杀 - 一般意义上的字典使我的头受伤了一些,因为我的背景更多在统计方面,数据具有非常明确的结构。我在Python 2.7中工作,我有以下问题:在一些分析结束时,使用NetworkX软件包,我有两个我感兴趣的字典,我称之为Able和Baker。



每个本质上都是一个网络节点名称的字典和一个值。所以这样的东西:



Joe:7凯特:9麦克:3 - 和乔:12凯特:4麦克:2



但是这个输出将会离开Python,虽然我可以很容易地将整个东西作为两个独立的文件输出(直到现在为止),但是将它作为一个单独的CSV文件格式如下:



Joe,7,12

凯特,9,4

Mike,3, 2



现在,我运行以下命令,将两个字典合并为单个字典:


$ b $对于A中的k,b

  output = dict((k,[Able [k],Baker.get(k)])
output.update((k, [无,贝克[k]])对于贝克的k,如果k不在高手中)

我靠近我想要的,即一个与乔相关的字典:[7,12]。但是老实说,如何让CSV作者合作并输出[7,12]作为不同的实体(即7,12)而不是数组的一部分。我最接近的是:

  net_out = open('output.csv','w')
作者= csv.writer(net_out,dialect ='excel')
为节点,output.items()中的值:
writer.writerow([node,value])
net_out.close ()

哪个排出Joe的行[7,12]。任何人都可以指向正确的方向?

解决方案

您正在传递一个包含名称和列表的列表,其中包含两个数字到 writer.writerow 。为了得到你想要的东西,把列表变成一个名字和数字的列表。简单的方法:

 为节点,output.items()中的值:
writer.writerow([node] +价值)


First question here, please forgive the various and sundry errors I'm sure to make.

I've been working on this for awhile, and I admit to being stumped - dictionaries in a general sense make my head hurt a bit, as my background is more on the statistical side, where data has pretty explicit structure. I'm working in Python 2.7, and I've got the following problem: At the end of some analysis, using the NetworkX package, I've got two dictionaries I'm interested in, which I'll call Able and Baker.

Each is essentially a dictionary of a network node's name, and a value. So something like:

Joe : 7 Kate: 9 Mike: 3 -and- Joe: 12 Kate: 4 Mike: 2

But this output is going to leave Python, and while I could output the whole thing as two separate files fairly easily (and have been up until now), it would be really nice to have it come out as a single CSV file, formatted like so:

Joe, 7, 12
Kate, 9, 4
Mike, 3, 2

Right now, I'm running the following to get the two dictionaries combined into a single dictionary:

output = dict((k, [Able[k], Baker.get(k)]) for k in Able)
output.update((k, [None, Baker[k]]) for k in Baker if k not in Able)

This gets me close to what I want, namely a dictionary with Joe: [7, 12]. But I'm honestly stumped as to how to get CSV writer to cooperate and output the [7,12] as distinct entities (i.e. 7,12) rather than as parts of an array. The closest I've gotten is:

net_out = open('output.csv', 'w')
writer = csv.writer(net_out, dialect='excel')
for node, value in output.items():
    writer.writerow([node,value])
net_out.close()

Which puts out rows of Joe, [7,12]. Anyone able to point me in the right direction?

解决方案

You're passing a list containing a name and a list with two numbers to writer.writerow. To get what you want, flatten the list into just a list of the name and numbers. Simple approach:

for node, value in output.items():
    writer.writerow([node] + value)

这篇关于将数组中的值输出到CSV文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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