将GraphML文件转换为另一个

时间:2017-01-19 07:08:50

标签: python performance lxml graphml iterparse

您好我有一个简单的graphML文件,我想从GraphML中删除节点标记并将其保存在另一个GraphML文件中。给出的GraphML大小是3GB以下的样本。

输入文件:

<?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
    <key id="weight" for="edge" attr.name="weight" attr.type="string"></key>
    <graph id="G" edgedefault="directed">
        <node id="1"></node>
        <node id="2">
        </node>
        <node id="3">
        </node>
        <node id="4">
        </node>
        <node id="5">
        </node>
        <edge id="6" source="1" target="2">
            <data key="weight">3</data>
        </edge>
        <edge id="7" source="2" target="4">
            <data key="weight">1</data>
        </edge>
        <edge id="8" source="2" target="3">
            <data key="weight">9</data>
        </edge>
    </graph>
</graphml>

必需输出:

<?xml version="1.0" ?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.1/graphml.xsd">
    <key id="weight" for="edge" attr.name="weight" attr.type="string"></key>
    <graph id="G" edgedefault="directed">
        <edge id="6" source="1" target="2">
            <data key="weight">3</data>
        </edge>
        <edge id="7" source="2" target="4">
            <data key="weight">1</data>
        </edge>
        <edge id="8" source="2" target="3">
            <data key="weight">9</data>
        </edge>
    </graph>
</graphml>

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

有一个python模块来处理graphml。 奇怪的是,documentation没有removedelete功能。

由于graphml是xml标记,因此您可以使用xml模块。 我已经使用了xmltodict并非常喜欢它。 此模块允许您将xml代码加载到python对象。修改对象后,可以将其保存回xml。

如果data是包含xml的字符串:

data_object=xmltodict.parse(data)
del data_object["graphml"]["graph"]["node"]
xmltodict.unparse(data_object, pretty=True)

这将删除node条目,unparse将返回带有xml的字符串。

如果xml的结构变得更复杂,您需要在data_object中搜索节点。但这不应该是一个问题,它只是一个有序的字典。

另一个问题可能是xml的大小。 3GB很多。 xmltodict支持大型文件的流式传输模式,但这是我从未使用过的。

答案 1 :(得分:0)

在阅读了一些链接之后,我想出了迭代解析的解决方案。在RAM使用方面,我无法弄清楚简单解析和iterparse之间的区别。

重要链接:
  - http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
  - using lxml and iterparse() to parse a big (+- 1Gb) XML file

代码:

将lxml.etree导入为

graphml = {  
   "graph": "{http://graphml.graphdrawing.org/xmlns}graph",  
   "node": "{http://graphml.graphdrawing.org/xmlns}node",  
   "edge": "{http://graphml.graphdrawing.org/xmlns}edge",  
   "data": "{http://graphml.graphdrawing.org/xmlns}data",  
   "weight": "{http://graphml.graphdrawing.org/xmlns}data[@key='weight']",  
   "edgeid": "{http://graphml.graphdrawing.org/xmlns}data[@key='edgeid']"  
}



for event, elem in et.iterparse("/data/sample.graphml",tag=graphml.get("edge"), events = ('end', )):  
    print(et.tostring(elem))
    elem.clear()
    while elem.getprevious() is not None:
        del elem.getparent()[0]