Answer the question
In order to leave comments, you need to log in
element.remove(subelement) firing once in a loop
Kind people, please tell me where I made a mistake ...
There is an xml of the form
<server>
<User>
<Username>user</Username>
<Password>pass</Password>
<list>
<Item c="ru" askstatus="-1" recvstatus="-1" substatus="3" name="Roman"> </Item>
<Item c="ru" askstatus="-1" recvstatus="-1" substatus="3" name="Roman"> </Item>
<Item c="ru" askstatus="-1" recvstatus="-1" substatus="3" name="Roman"> </Item>
<Item c="com askstatus="-1" recvstatus="-1" substatus="3" name="Roman"> </Item>
</list>
</User>
</server>
#!/usr/bin/env python
import xml.etree.ElementTree as etree
import sys
tree = etree.parse(sys.argv[1])
root = tree.getroot()
for user in root:
for param in user:
if param.tag == 'list':
for item in param:
if str(item.get('c')) == 'ru':
param.remove(item)
tree.write('output.xml')
Answer the question
In order to leave comments, you need to log in
If you look at ElementTree.__getitem_(self, index), you can see that with each iteration of item, index is passed to params: the first iteration is 0, the second is 1, and dt
def __getitem__(self, index ):
return self._children[index]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question