[英]How to access attribute value in xml containing namespace using ElementTree in python
[英]How to get attribute value of next child in XML tree using ElementTree
我正在使用ElementTree解析XML文件。 但是我不知道在解析时如何访问下一个元素。 我想做的是,如果对应于下一个孩子的相同标签相同,则将特定标签打印到文件中。 例如。 如果第五个元素的code
标签是12345,而第六个元素的code
标签也是12345,那么我想将代码打印在文件中。
就像是:
for child in root:
if child.find('code').text == next(child).find('code').text:
file.write(child.find('code').text + "\n")
这已经过测试,应该可以工作:
it = root.iter('code')
prev = it.next()
for current in it:
if prev.text == current.text:
print prev.text
prev = current
您也可以使用zip
:
for current, next in zip(codes, codes[1:]):
print map(lambda node: node.find('code').text, [current, next])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.