Python Networkx无法导出到具有属性的graphml [英] Python Networkx can't export to graphml with attributes

查看:208
本文介绍了Python Networkx无法导出到具有属性的graphml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将networkx图转换为graphml时存在以下问题(此处无法发布所有代码,但要点在于此). NetworkX版本是1.6.2

Having the following problem converting a networkx graph to a graphml (can't post all the code here but here is the gist). NetworkX version is 1.6.2

我有一个叫做G的networkx有向图.

I have a networkx digraph called G

G = nx.DiGraph()

使用来自作者列表的加权边将其填充(格式为AuthororA,AuthorB,Weight)

Populate it with weighted edges from a list of authors in the format (AuthorA, AuthorB, Weight)

G.add_weighted_edges_from(author_list)

我可以在这里导出graphml,并且效果很好

I can export the graphml here and it works fine

nx.write_graphml(G, 'test.graphml')

然后我在图表上计算网页排名

I then calculate the pagerank on the graph

graph_metric = nx.pagerank_numpy(G, weight='weight')

然后将属性添加到图中的节点

and then add attributes to the nodes in the graph

nx.set_node_attributes(G, 'pagerank', graph_metric)

如果我遍历图,则可以打印出节点名称和pagerank

if I iterate over the graph I can print out the node name and the pagerank

for n.d in G.nodes_iter(data=True):
     print n, d

作者A {u'pagerank':0.0076688948270074164} ... ... ...

AuthorA {u'pagerank': 0.0076688948270074164} ... ... ...

但是如果更新属性后,我尝试从图形中创建一个graphml,则会出现以下错误:

but if after updating the attributes I try to create a graphml from the graph I get the following error:

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 111, in generate_graphml

writer.add_graph_element(G)

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 305, in add_graph_element

self.add_nodes(G,graph_element)

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 262, in add_nodes

self.add_attributes("node", node_element, data, default)

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 255, in add_attributes

scope=scope, default=default_value)

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 242, in add_data

raise nx.NetworkXError('GraphML writer does not support '

NetworkXError: GraphML writer does not support dict types as data values.

有想法吗?

推荐答案

它确实告诉您-您不能将字典作为graphml属性-在大多数情况下,它们必须是数字类型或字符串.

well, it does tell you - you can't put a dictionary as a graphml attribute - they need to be either numeric types or strings, for the most part.

不是答案,但是更容易在此处放置代码. 找到坏"节点:

not the answer, but easier to put code here. find the 'bad' node:

for node in G.node:
    for attrib in G.node[node]:
        if type(G.node[node][attrib]) == dict:
            print node

这篇关于Python Networkx无法导出到具有属性的graphml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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