当您从其父节点中删除节点时,该节点本身仍然存在,但只是与父节点分离。这允许您将“已删除”节点附加到不同的父节点。但是如果你不将节点附加到一个新的父节点,那么从根节点的角度来看,该节点是一个很好的删除节点。
为了保留被移除的标签节点的子节点,你可以像这样在相同的索引处将它们修剪到标签的父节点:
parent = tag.getparent()
index = parent.index(tag)
for child in tag.getchildren()[::-1]: # in reverse order so that we can keep inserting at the same index while preserving the original order
tag.remove(child)
parent.insert(index, child)
parent.remove(tag)
或者您可以简单地使用该drop_tag
方法:
tag.drop_tag()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句