Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用lxml迭代GraphML文件_Python_Lxml_Loops_Graphml - Fatal编程技术网

Python 如何使用lxml迭代GraphML文件

Python 如何使用lxml迭代GraphML文件,python,lxml,loops,graphml,Python,Lxml,Loops,Graphml,我有下面的GraphML文件“mygraph.gml”,我想用一个简单的python脚本来解析它: 这表示一个简单的图,其中有两个节点“node0”、“node1”以及它们之间的一条边 <?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i

我有下面的GraphML文件“mygraph.gml”,我想用一个简单的python脚本来解析它:

这表示一个简单的图,其中有两个节点“node0”、“node1”以及它们之间的一条边

<?xml version="1.0" encoding="UTF-8"?>
<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.0/graphml.xsd">
  <key id="name" for="node" attr.name="name" attr.type="string"/>
  <key id="weight" for="edge" attr.name="weight" attr.type="double"/>
  <graph id="G" edgedefault="directed">
    <node id="n0">
      <data key="name">node1</data>
    </node>
    <node id="n1">
      <data key="name">node2</data>
    </node>
<edge source="n1" target="n0">
  <data key="weight">1</data>
</edge>
  </graph>
</graphml>
这个脚本可以正确地获取节点和边,这样我就可以简单地对它们进行迭代

for n in nodes:
    print n.attrib
或类似地在边缘上:

for e in edges:
    print (e.attrib['source'], e.attrib['target'])
但我真的不明白如何获取边或节点的“数据”标记,以便打印边权重和节点标记“名称”

这对我不起作用:

weights = graph.findall(graphml.get("weight"))

最后一个列表总是空的。为什么?我遗漏了一些东西,但不明白是什么。

您不能一次完成,但对于找到的每个节点,您可以使用数据的键/值构建dict:

graph = tree.find(graphml.get("graph"))
nodes = graph.findall(graphml.get("node"))
edges = graph.findall(graphml.get("edge"))

for node in nodes + edges:
    attribs = {}
    for data in node.findall(graphml.get('data')):
        attribs[data.get('key')] = data.text
    print 'Node', node, 'have', attribs
结果如下:

Node <Element {http://graphml.graphdrawing.org/xmlns}node at 0x7ff053d3e5a0> have {'name': 'node1'}
Node <Element {http://graphml.graphdrawing.org/xmlns}node at 0x7ff053d3e5f0> have {'name': 'node2'}
Node <Element {http://graphml.graphdrawing.org/xmlns}edge at 0x7ff053d3e640> have {'weight': '1'}
节点具有{'name':'node1'}
节点具有{'name':'node2'}
节点具有{'weight':'1'}

谢谢!这绝对是我一直在寻找的解决方案!再次感谢,现在我了解了树的结构以及如何在树上迭代。
Node <Element {http://graphml.graphdrawing.org/xmlns}node at 0x7ff053d3e5a0> have {'name': 'node1'}
Node <Element {http://graphml.graphdrawing.org/xmlns}node at 0x7ff053d3e5f0> have {'name': 'node2'}
Node <Element {http://graphml.graphdrawing.org/xmlns}edge at 0x7ff053d3e640> have {'weight': '1'}