简体   繁体   中英

Removing elements from an aml using ElementTree in python

So I have an xml

<a>
    <b><\b>
    <b><\b>
</a>

and I run

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))

expecting <a></a> as output but I get this <a><b /></a>

What am I misunderstanding here?

Try changing

for x in root:

to

for x in root.findall('*'):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM