【发布时间】:2020-05-08 13:20:01
【问题描述】:
示例 XML:
<ParentTag TEST_ID="xxxxxx" ID="1">
<Child TagType="Manual">
<Manhrs Cost="100"/>
<Testing VAL="RANDOM STRING"/>
</Child>
<Child TagType="Automated">
<Manhrs Cost="10"/>
<Testing VAL="RANDOM STRING2"/>
</Child>
</ParentTag>
<ParentTag TEST_ID="YYYYYY" ID="1">
<Child TagType="Manual">
<Manhrs Cost="100"/>
<Testing VAL="RANDOM STRING"/>
</Child>
<Child TagType="Automated">
<Manhrs Cost="10"/>
<Testing VAL="RANDOM STRING2"/>
</Child>
</ParentTag>
<ParentTag TEST_ID="ZZZZZZZ" ID="1">
<Child TagType="Manual">
<Manhrs Cost="100"/>
<Testing VAL="RANDOM STRING"/>
</Child>
<Child TagType="Automated">
<Manhrs Cost="10"/>
<Testing VAL="RANDOM STRING2"/>
</Child>
</ParentTag>
所以如果我运行:
TEST_ID = xxxxxx
for n in root.findall("ParentTag[@TEST_ID = '%s']//Child[@TagType = 'Automated']", % TEST_ID):
print("FOUND")
错误:python2.7/xml/etree/ElementPath.py",第 224 行,在 prepare_predicate 中 引发 SyntaxError("无效谓词") SyntaxError: 无效谓词
请注意,上面相同的代码在 python 3 中确实可以工作,但我需要它在 Python 2.7 中工作 如果除了在 python 2 中循环多次之外还有其他选择,请提供相同的方法
我还必须使用 xml.etree.ElementTree
【问题讨论】:
标签: python-2.7 xml.etree