Python2.6中ElementTree的iter()等效项 [英] ElementTree's iter() equivalent in Python2.6

查看:178
本文介绍了Python2.6中ElementTree的iter()等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ElementTree的这段代码,可与Python 2.7很好地配合。
我需要获取 X / Y节点下所有名称为 A的节点。

I have this code with ElementTree that works well with Python 2.7. I needed to get all the nodes with the name "A" under "X/Y" node.

from xml.etree.ElementTree import ElementTree

verboseNode = topNode.find("X/Y")
nodes = list(verboseNode.iter("A"))

当我尝试使用Python 2.6运行它时,出现了此错误。

However, when I tried to run it with Python 2.6, I got this error.

ionCalculateSkewConstraint.py", line 303, in getNodesWithAttribute
    nodes = list(startNode.iter(nodeName))
AttributeError: _ElementInterface instance has no attribute 'iter'

看起来像Python 2.6 ElementTree的节点
如何使用Python 2.6实现iter()?

It looks like that Python 2.6 ElementTree's node doesn't have the iter(). How can I implement the iter() with Python 2.6?

推荐答案

请注意, iter 在Python 2.6(甚至2.5)中可用-否则,文档),因此您实际上不需要替换。

Note that iter is available in Python 2.6 (and even 2.5 - otherwise, there'd be a notice in the docs), so you don't really need a replacement.

但是,您可以使用 findall

You can, however, use findall:

def _iter_python26(node):
  return [node] + node.findall('.//*')

这篇关于Python2.6中ElementTree的iter()等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆