How to remove a node inside an iterator in python xml.etree.ElementTree

你说的曾经没有我的故事 提交于 2019-12-19 09:59:52

问题


How to remove the current node, while iterating through all nodes from root by getiterator() function?

import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()

for node in root.getiterator():
     #if some condition:
        #remove(node)

回答1:


You can't remove nodes without knowing the parent, but the xml.etree package doesn't give you any way to access a parent from a given node.

The only way around this is matching the parent node instead:

for node in root.iter():
    if some_condition_matches_parent:
        for child in list(node.iter()):
            if some_condition_matches_child:
                node.remove(child)

If you switch to the lxml library (which implements the same API, but with additional enhancements), you can retrieve the parent node from any given node:

node.getparent().remove(node)

Note, while the pure-Python implementation of Element.getiterator() returns a list object, in the C implementation of the ElementTree module (a separate import on Python 2, transparently imported on Python 3 if available) the getiterator() method returns a live generator which requires a copy to be made.

On top of that, the Element.getiterator() method has been deprecated in Python 3.2 and will be removed altogether in Python 3.9. I replaced its use with node.iter() in the outer loop, and list(node.iter()) in the inner.



来源:https://stackoverflow.com/questions/19419754/how-to-remove-a-node-inside-an-iterator-in-python-xml-etree-elementtree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!