gpt4 book ai didi

python - 我如何评估 python 集合上的路径表达式,就像我可能对 XML 文档使用 XPath 一样?

转载 作者:太空宇宙 更新时间:2023-11-04 03:22:17 24 4
gpt4 key购买 nike

XML 具有 XPath。 Perl 有数据::路径。 python 中是否存在任何东西来评估 python 对象或至少集合的路径表达式?

考虑这个集合:

deployments = [
{'type': 'database',
'owner': 'fred',
'server': 'db-F'},
{'type': 'database',
'owner': 'carol',
'server': 'db-W'},
{'type': 'application',
'owner': 'carol',
'server': 'web-A'},
{'type': 'application',
'owner': 'anthony',
'server': 'web-A'} ]

如果我能写一个像这样调用的“path_search”函数会很有帮助:

path_search(deployments, ".//*[owner = 'carol']")

返回这个:

[   {'type': 'database',
'owner': 'carol',
'server': 'db-W'},
{'type': 'application',
'owner': 'carol',
'server': 'web-A'} ]

路径优于硬编码结构遍历的原因是我的配置在结构和嵌套方面因文件而异。

最佳答案

  1. 有一个dpath PyPI 中的库。

A python library for accessing and searching dictionaries via /slashed/paths ala xpath

但该库似乎只专注于在嵌套词典中进行搜索。

  1. 还有一个jsonpath-rw支持不同的搜索表达式来查找项目的库。

关于python - 我如何评估 python 集合上的路径表达式,就像我可能对 XML 文档使用 XPath 一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34298743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com