在Python中处理yEd的graphml文件

2024-06-01 10:48:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个yEd创建的graphml文件中得到一个所有节点和一些属性(例如标签名)的列表,而不管它们位于图中的哪个位置。这个问题已经有一部分处理过了(Processing XML file with networkx in pythonHow to iterate over GraphML file with lxml),但是当你在yEd中“分组”节点时就没有了——我在分组中有很多分组。在

已经尝试过networkx和lxml,但没有使用建议的简单方法得到完整的结果-关于优雅的解决方法和使用哪个库的建议-没有递归迭代遍历树和标识组节点并再次深入研究。在

示例:

使用networkx对非常简单的图形进行分组时的输出示例:

('n0', {})
('n1', {'y': '0.0', 'x': '26.007967509920633', 'label': 'A'})
('n0::n0', {})
('n0::n1', {})

Simple representation of the graph


Tags: 文件方法networkx示例属性节点with标签
2条回答

我想你可以试试这个。在

这是一个Python库,根据作者的说法。。。在

provides an easy interface that lets you specify how a graph should look, and generates corresponding graphML that can be opened in yEd.

https://github.com/jamesscottbrown/pyyed

希望这有帮助!在

干杯!在

在尝试了networkx、lxml和pygraphml之后,我决定他们根本不做这项工作。我用BeautifulSoup从头开始写:

from bs4 import BeautifulSoup

fp = "files/tes.graphml"

with open(fp) as file:
    soup = BeautifulSoup(file, "lxml")

    nodes = soup.findAll("node", {"yfiles.foldertype":""})
    groups = soup.find_all("node", {"yfiles.foldertype":"group"})
    edges = soup.findAll("edge")

然后你会得到这样的结果:

^{pr2}$

这应该能让你走了。您可以创建组、节点和边缘对象,并将它们用于某些处理。在

我可能会打开我正在工作的库的源代码,因为它将用于一个更大的目的,而不仅仅是解析图形。在

enter image description here

以及输出:

  - Groups  - 
n0 / SimpleApp
  - Nodes  - 
n0::n0 / main
n0::n1 / say hello
n1 / Exit
  - Edges  - 
n0::e0 / n0::n0 / n0::n1 / str:username, int:age
e0 / n0::n1 / n1 / None

相关问题 更多 >