Python的Networkx不能导出与属性graphml(Python Networkx can&

2019-10-19 05:21发布

具有下列问题networkx图形转换成graphml(不能在这里发布的所有代码,但这里的要点)。 NetworkX版本是1.6.2

我有一个称为图G networkx有向图

G = nx.DiGraph()

与来自作者的列表格式加权边缘填充它(作者单位,AuthorB,重量)

G.add_weighted_edges_from(author_list)

我在这里可以导出graphml和正常工作

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

然后,我计算出的曲线图中的PageRank

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

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

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

如果我遍历图我可以打印出节点名称和歌的PageRank

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

{作者单位u'pagerank':0.0076688948270074164} ... ... ...

但是,如果更新后的属性我尝试创建从图中我得到了以下错误graphml:

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.

思考?

Answer 1:

好了,它告诉你 - 你不能够把一个字典作为一个graphml属性 - 他们需要是数值类型或字符串,大部分。

没有答案,但更容易把这里的代码。 找到“坏”节点:

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


文章来源: Python Networkx can't export to graphml with attributes