我编写了一个代码,从tes.xml
中删除那些不在列表lis
中的国家,并在删除这些国家后生成更新的xmloutput.xml
。但是在生成输出xml时出错
XML:
tes.xml
<?xml version="1.0"?>
<data>
<country>
<state>
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</state>
</country>
<country>
<state>
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<gpc>59900</gpc>
<neighbor name="Malaysia" direction="N"/>
</state>
</country>
<country>
<state>
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</state>
</country>
</data>
代码:
import xml.etree.ElementTree as ET
tree = ET.parse('tes.xml')
lis = ['5', '2']
root = tree.getroot()
for country in root.findall('.//country/state'):
rank = str(country.find('rank').text)
print(rank)
if rank not in lis:
root.remove(country)
tree.write('outpu.xml')
错误:
2
5
69
Traceback (most recent call last):
File "main.py", line 11, in <module>
root.remove(country)
ValueError: list.remove(x): x not in list
预期产出:
<data>
<country>
<state>
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</state>
</country>
<country>
<state>
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<gpc>59900</gpc>
<neighbor name="Malaysia" direction="N"/>
</state>
</country>
</data>
root.remove()
仅删除根元素的直接子元素。<country>
元素确实是直接子元素,但是代码中的country
变量引用了<state>
元素这项工作:
相关问题 更多 >
编程相关推荐