用Python解析GXL [英] Parsing GXL in Python
问题描述
我需要将GXL文件(一种用于图形表示的XML)解析为python.我有一个最简单的例子:
I need to parse a GXL file (a kind of XML for graph representation) into python. I have the simplest example:
<node id="V0001">
<attr name="name">
<string>V0001</string>
</attr>
</node>
<node id="V0002">
<attr name="name">
<string>B</string>
</attr>
</node>
<edge from="V0001" to="V0002">
<attr name="name">
<string>E0001</string>
</attr>
</edge>
这给出了一个简单的图,其中节点V0001和V0002与一条称为E0001的边相连.
That gives a simple graph with nodes V0001 and V0002 connected with an edge called E0001.
我需要程序做的是调用我已经拥有的2个函数.首先是:
What I need the program to do is to call 2 functions, that i have already. First is:
dw(a)
这将使用networkx库添加一个节点(并做一些其他事情供以后使用,这就是我自己定义它的原因).我希望每个节点都使用其名称而不是 a
which adds a node using networkx library (and does a few more things for later use, that's why I have defined it myself). I want it to be called for each node with its name instead of a
另一个是:
dk(a,b)
在 a 和 b
我知道我可以使用内置的python XML处理库,我可以导入该库:
I know I can use a built-in python XML handling library, which I do import:
from xml.dom.minidom import parse
是否有一种优雅,清晰的方法?所以我得到了一个不错的可读代码?
Is there an elegant, clear way to do so? So I get a nice, readable code?
推荐答案
以防万一以后有人找它,这就是我的dk(用于读取多个标签)如何与相同的 xml.etree.ElementTree 库:
Just in case anyone looked for it later, here is how my dk (for reading multiple tags) works with the same xml.etree.ElementTree library:
for edge in tree.findall(".//edge"):
start = edge.get('from')
end = edge.get('to')
dk(start,end)
这篇关于用Python解析GXL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!