如何以graphml格式保存MultiDigraph?

时间:2021-02-12 08:20:01

标签: python networkx social-networking

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

我使用了 bellow 命令,但没有用。此命令似乎不适用于 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'>

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

看起来您的属性之一属于 datetime 类型。但是,从 networkx 的实现中可以看出,仅支持以下类型(请参阅 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 对象,例如创建字符串表示。