I have an XML file of the form:
<PlayersList>
<player>
<username>Someuser</username>
<network>Somenetwork</network>
<country>France</country>
<blocked>False</blocked>
<resetDate>NA</resetDate>
<Stat1>1665</Stat1>
<Stat2>-5.47</Stat2>
<Stat3>15.07</Stat3>
<Stat4>-16.8</Stat4>
<Stat5>-9104.47</Stat5>
<Stat6>25087.75</Stat6>
<Stat7>17965.79</Stat7>
<Stat8>2011-04-17 03:33</Stat8>
<Stat9>28</Stat9>
<Stat10>5.9</Stat10>
<Stat11>283</Stat11>
<Stat12>-33.6</Stat12>
<Stat13>22.3</Stat13>
<Stat14>460.4</Stat14>
<Stat15>57.5</Stat15>
</player>
.
.
</PlayersList>
I'm trying to delete all the child nodes of player. I select the given player element I want with
e= root.findall('player')[2]
then attempt to delete all childen:
for child in e:
e.remove(child)
Yet only a few child nodes are removed each time that I run this, not all! Why?
I can list all child tags and their text no problem with
for child in e:
print child.tag+':'+child.text
This is because you are modifying the element you are iterating on. You can store the child elements in a temporary list and iterate over that instead:
for child in list(e):
e.remove(child)
This will remove all children.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With