I have a graph constructed using the networkx package in Python that has data attributes associated with both nodes and edges. These attributes are dictionaries (or lists) themselves with nested dictionaries. I can't figure out how to write this graph to .gexf format (or .graphml etc) because of the data type.
Is there a way to get write_gexf to parse these data types to XML? or is there some other workaround?
Here's an example:
1 import networkx as nx
2
3 G = nx.graph()
4 G.add_node(0, attr1 = { 'name1' : 'Alice', 'name2' : 'Bob' }, attr2 = 5)
5 G.add_node(0, attr1 = { 'name1' : 'Calvin', 'name2' : 'Hobbes' }, attr2 = 6)
6 G.add_edge(0,1, likes = ['milk', 'oj'])
7
8 nx.write_gefx(G,"test.gefx")
which gives an error:
Traceback (most recent call last):
File "so_write_gefx.py", line 8, in <module>
nx.write_gexf(G,"test.gexf")
...
line 378, in add_attributes
for val,start,end in v:
ValueError: too many values to unpack
The GEXF format only specifies a set if fairly simple data types. In your example you are setting the edge data attribute "likes" to be a list
G.add_edge(0,1, likes = ['milk', 'oj'])
which is not handled by the GEXF writer.
If you stick to strings, numbers, etc then you won't encounter this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With