gpt4 book ai didi

python - ElementTree XPath - 根据属性选择元素

转载 作者:IT老高 更新时间:2023-10-28 21:35:32 26 4
gpt4 key购买 nike

我在使用 ElementTree 中的属性 XPath Selector 时遇到问题,根据 Documentation 应该可以做到这一点。

这里有一些示例代码

XML

<root>
<target name="1">
<a></a>
<b></b>
</target>
<target name="2">
<a></a>
<b></b>
</target>
</root>

Python

def parse(document):
root = et.parse(document)
for target in root.findall("//target[@name='a']"):
print target._children

我收到以下异常:

expected path separator ([)

最佳答案

您尝试使用的语法是 ElementTree 1.3 中的新语法

此类版本随 Python 2.7 或更高版本一起提供。如果你有 Python 2.6 或更低版本,你仍然有 ElementTree 1.2.6 或更低版本。

关于python - ElementTree XPath - 根据属性选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/222375/

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