Problem: Ich möchte einen Tag in einer xml Datei entfernen:
Entfernt werden sollen die <year> tags
Hier die xml Datei
Code: Alles auswählen
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<date>
<year>2008</year>
<month>12</month>
</date>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<date>
<year>2009</year>
<month>12</month>
</date>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<date>
<year>2010</year>
<month>12</month>
</date>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
Code: Alles auswählen
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
// das funktioniert
for year in root.iter('year'):
print year.tag
// aber das nicht
for date in root.iter('data'):
root.remove(year.tag)
tree.write('out.xml')
Code: Alles auswählen
Traceback (most recent call last):
File "<pyshell#31>", line 2, in <module>
root.remove(year.tag)
File ".\Python27\lib\xml\etree\ElementTree.py", line 337, in remove
self._children.remove(element)
ValueError: list.remove(x): x not in list