问题
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"))
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'
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?
回答1:
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.
You can, however, use findall:
def _iter_python26(node):
return [node] + node.findall('.//*')
回答2:
Not sure if this is what you are looking for, as iter()
appears to be around in 2.6, but there's getiterator()
http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.getiterator
来源:https://stackoverflow.com/questions/7616800/elementtrees-iter-equivalent-in-python2-6