将networkx图转换为graphml时遇到以下问题(无法在此处发布所有代码,但这是要点)。NetworkX 版本为 1.6.2
我有一个名为 G 的 networkx 有向图
G = nx.DiGraph()
使用格式(AuthorA,AuthorB,Weight)的作者列表中的加权边缘填充它
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
作者A {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.
想法?