如何以 graphml 格式保存 MultiDigraph?

How can I save MultiDigraph in graphml format?

我正在使用 python 包 networkx 并且有一个 MultiDigraph 我想将其保存为 graphml 格式以便在其他软件中使用它以供我进一步分析。我也有一些节点和边缘属性。

我使用了波纹管命令,但没有用。似乎此命令不适用于 MultiDigraph.

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

这是我得到的错误:

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'>

有人能帮忙吗?

您的属性之一似乎是 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"),
        ]

因此您需要手动替换 datetime 对象,例如创建字符串表示形式。