使用 python 从 XML 中仅获取想要的元素时出现错误 "list.remove(x): x not in list"
Getting error "list.remove(x): x not in list" while fetching only wanted elements from XML using python
我编写了一个代码,用于从 tes.xml
中删除列表 lis
中不存在的那些排名的国家,并在删除国家后生成更新的 xml output.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>
元素。
这个有效:
for country in root.findall('.//country'):
rank = country.find('state/rank').text
print(rank)
if rank not in lis:
root.remove(country)
我编写了一个代码,用于从 tes.xml
中删除列表 lis
中不存在的那些排名的国家,并在删除国家后生成更新的 xml output.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>
元素。
这个有效:
for country in root.findall('.//country'):
rank = country.find('state/rank').text
print(rank)
if rank not in lis:
root.remove(country)