在python中处理yed graphml文件 - python

我想在yEd创建的graphml文件中获取所有节点和一些属性(例如标签名称)的列表,无论它们在图中的位置如何。这已经部分解决了(Processing XML file with networkx in python和How 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', {})

参考方案

在试用了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")

然后,您将得到如下结果:

print " --- Groups --- "
for group in groups:
    print group['id']
    print group.find("y:nodelabel").text.strip()

print " --- Nodes --- "
for node in nodes:
    print node['id']
    print node.find("y:nodelabel").text.strip()

这应该可以帮助您。您可以制作“组”,“节点和边缘”对象并将其用于某些处理。

我可以开源我正在使用的库,因为它将用于更大的目的,而不仅仅是解析图。

在python中处理yed graphml文件 - python

并输出:

 --- 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

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

无法注释掉涉及多行字符串的代码 - python

基本上,我很好奇这为什么会引发语法错误,以及如何用Python的方式来“注释掉”我未使用的代码部分,例如在调试会话期间。''' def foo(): '''does nothing''' ''' 参考方案 您可以使用三重双引号注释掉三重单引…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…