[英]Converting GraphML file to another
嗨,我有一个简单的 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>
有没有办法做到这一点?
有一个python模块来处理graphml。 奇怪的是,该文档没有remove
或delete
功能。
由于 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 确实支持大文件的流模式,但这是我从未使用过的。
在阅读了一些链接之后,我想出了迭代解析的解决方案。 Bt 我无法弄清楚简单解析和迭代解析在 RAM 使用方面的区别。
重要链接:
- http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
- 使用 lxml 和 iterparse() 来解析一个大 (+- 1Gb) XML 文件
代码 :
导入 lxml.etree as et
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.