如何在python xml.etree.ElementTree中的迭代器中删除节点 [英] How to remove a node inside an iterator in python xml.etree.ElementTree

查看:728
本文介绍了如何在python xml.etree.ElementTree中的迭代器中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过getiterator()函数从根目录遍历所有节点的同时,如何删除当前节点?

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)

推荐答案

您无法在不知道父节点的情况下删除节点,但是xml.etree包无法为您提供从给定节点访问父节点的任何方式.

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)

如果切换到lxml库(该库实现相同的API,但具有其他增强功能),则可以从任何给定节点中检索父节点:

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)

注意,虽然Element.getiterator()的纯Python实现返回一个列表对象,但是在ElementTree模块的C实现中(在Python 2上是单独的导入,如果可用,则在Python 3上透明地导入),getiterator()方法将返回需要复制的实时生成器.

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.

最重要的是,Element.getiterator()方法已在Python 3.2中弃用,并将在Python 3.9中完全删除.我在外部循环中用node.iter()替换了它的用法,在内部循环中用list(node.iter())替换了它.

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.

这篇关于如何在python xml.etree.ElementTree中的迭代器中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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