pythonxmlnumpynetworkxgraphml

What data types can be exported as attributes when exporting to `graphml` with `networkx`?


I have a networkx graph with some list and dict attributes that the function nx.write_graphml doesn't accept as valid type for the export. I couldn't find a function that would automatically ignore those attributes so I decided to create one myself.

I know that int, float and string are accepted, but I don't know if there are others and, if so, which ones.


Solution

  • The graphml format follows XML structure and according to the docs, the following data types are accepted:

    Each attribute title is defined in a key element with an identifier, a name, a title, edge or node, and the type of data. The supported data types:

    • boolean
    • int
    • long
    • float
    • double
    • string

    If the lists are not too long, then one suggestion is to create numbered attributes (e.g. colour_1, colour_2). Similarly, for dictionaries. This is not convenient, but these are graphml constraints.

    Also, if you do not intend to use the list or dict in Gephi, but want to preserve them for later use, then one option is to convert them to a string representation using something like json.dumps.

    Update: when writing the data, networkx will try to convert the data types to the allowed representations. For example, the list of numpy-specific conversions can be found here.