Using nx.write_gexf in Python for graphing with dict data on nodes and edges

I have a graph built 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 cannot figure out how to write this graph in .gexf (or .graphml, etc.) format due to the data type.

Is there a way to get write_gexf to parse these data types in XML? or is there any other workaround?

Here is 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 
+4
source share
1 answer

The GEXF format indicates only a set if simple data types are sufficient. In your example, you set the boundary data attribute β€œlikes” as a list

 G.add_edge(0,1, likes = ['milk', 'oj']) 

which is not processed by the author of GEXF.

If you stick to strings, numbers, etc., you will not run into this problem.

+3
source

Source: https://habr.com/ru/post/1487561/


All Articles