在 python 中使用 ElementTree 从 aml 中删除元素
Removing elements from an aml using ElementTree in python
所以我有一个xml
<a>
<b><\b>
<b><\b>
</a>
我运行
import xml.etree.ElementTree
et = xml.etree.ElementTree.parse('abc.xml')
root = et.getroot()
for x in root:
root.remove(x)
print(xml.etree.ElementTree.tostring(root))
期待 <a></a>
作为输出,但我得到这个 <a><b /></a>
我误会了什么?
尝试更改
for x in root:
至
for x in root.findall('*'):
所以我有一个xml
<a>
<b><\b>
<b><\b>
</a>
我运行
import xml.etree.ElementTree
et = xml.etree.ElementTree.parse('abc.xml')
root = et.getroot()
for x in root:
root.remove(x)
print(xml.etree.ElementTree.tostring(root))
期待 <a></a>
作为输出,但我得到这个 <a><b /></a>
我误会了什么?
尝试更改
for x in root:
至
for x in root.findall('*'):