Search code examples
pythonnetworkxsocial-networking

How can I save MultiDigraph in graphml format?


I'm using the python package networkx and have a MultiDigraphwhich I want to save it in graphml format to use it in another software for my further analysis. I have sone nodes and edges attributes also.

I used the bellow command but it didn't work. It seems this command doesn't work for MultiDigraph.

nx.write_graphml(G, 'C:\\hm\\HMGraph.gml')

Here are the errors I get:

nx.write_graphml(G, 'C:\\hm\\HMGraph.gml')
Traceback (most recent call last):

  File "<ipython-input-5-e35c8921ea4a>", line 1, in <module>
    nx.write_graphml(G, 'C:\\hm\\HMGraph.gml')

  File "<decorator-gen-809>", line 2, in write_graphml_lxml

  File "C:\Users\xparve\Anaconda3\lib\site-packages\networkx\utils\decorators.py", line 239, in _open_file
    result = func_to_be_decorated(*new_args, **kwargs)

  File "C:\Users\xparve\Anaconda3\lib\site-packages\networkx\readwrite\graphml.py", line 153, in write_graphml_lxml
    writer = GraphMLWriterLxml(

  File "C:\Users\xparve\Anaconda3\lib\site-packages\networkx\readwrite\graphml.py", line 664, in __init__
    self.add_graph_element(graph)

  File "C:\Users\xparve\Anaconda3\lib\site-packages\networkx\readwrite\graphml.py", line 714, in add_graph_element
    T = self.xml_type[self.attr_type(k, "edge", v)]

KeyError: <class 'datetime.datetime'>

Can anyone help?


Solution

  • It looks like one of your attributes is of type datetime. However, as you can see from the implementation of networkx only the following types are supported (see GraphML class here)

            types = [
                (int, "integer"),  # for Gephi GraphML bug
                (str, "yfiles"),
                (str, "string"),
                (int, "int"),
                (int, "long"),
                (float, "float"),
                (float, "double"),
                (bool, "boolean"),
            ]
    

    So you need to manually replace the datetime objects by e.g. creating string representations.