本文介绍了如何删除lxml中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用python的lxml根据属性的内容完全删除元素.示例:
I need to completely remove elements, based on the contents of an attribute, using python's lxml. Example:
import lxml.etree as et
xml="""
<groceries>
<fruit state="rotten">apple</fruit>
<fruit state="fresh">pear</fruit>
<fruit state="fresh">starfruit</fruit>
<fruit state="rotten">mango</fruit>
<fruit state="fresh">peach</fruit>
</groceries>
"""
tree=et.fromstring(xml)
for bad in tree.xpath("//fruit[@state=\'rotten\']"):
#remove this element from the tree
print et.tostring(tree, pretty_print=True)
我要打印:
<groceries>
<fruit state="fresh">pear</fruit>
<fruit state="fresh">starfruit</fruit>
<fruit state="fresh">peach</fruit>
</groceries>
有没有一种方法可以执行此操作而无需存储临时变量并手动将其打印为:
Is there a way to do this without storing a temporary variable and printing to it manually, as:
newxml="<groceries>\n"
for elt in tree.xpath('//fruit[@state=\'fresh\']'):
newxml+=et.tostring(elt)
newxml+="</groceries>"
推荐答案
使用方法:
tree=et.fromstring(xml)
for bad in tree.xpath("//fruit[@state=\'rotten\']"):
bad.getparent().remove(bad) # here I grab the parent of the element to call the remove directly on it
print et.tostring(tree, pretty_print=True, xml_declaration=True)
如果我必须与@Acorn版本进行比较,即使要删除的元素不是直接位于xml根节点下,我的也可以正常工作.
If I had to compare with the @Acorn version, mine will work even if the elements to remove are not directly under the root node of your xml.
这篇关于如何删除lxml中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!