由于出现错误"ValueError:list.remove(x):x not in list"而无法从XML提取元素;在python中 [英] Unable to fetch elements from XML as getting error "ValueError: list.remove(x): x not in list" in python
问题描述
我写了一个代码,从 tes.xml
中删除列表 lis
中不存在的那些国家/地区,并生成更新的xml output.xml
删除国家/地区后.但是在生成输出xml时出错XML:
I have written a code to remove countries of those ranks which are not present in list lis
from tes.xml
and generating updated xml output.xml
after removing the countries. But getting error while generating the output xml
XML:
tes.xml
<?xml version="1.0"?>
<data>
<continents>
<country>
<state>
<rank updated="yes">123456</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</state>
<zones>
<pretty>yes</pretty>
</zones>
</country>
<country>
<state>
<rank updated="yes">789045</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<gpc>59900</gpc>
<neighbor name="Malaysia" direction="N"/>
</state>
<zones>
<pretty>No</pretty>
</zones>
<market>
<pretty>cool</pretty>
</market>
</country>
<country>
<state>
<rank updated="yes">67846464</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<gpc>59900</gpc>
<neighbor name="Malaysia" direction="N"/>
</state>
<zones>
<pretty>No</pretty>
</zones>
<market>
<pretty>cool</pretty>
</market>
</country>
</continents>
</data>
代码:
import xml.etree.ElementTree as ET
tree = ET.parse('tes.xml')
lis = ["123456"]
root = tree.getroot()
print('root is', root)
print(type(root))
for continent in root.findall('.//continents'):
for country in continent:
rank = country.find('state/rank').text
print(rank)
if rank not in lis:
continent.remove(country)
tree.write('outpu.xml')
控制台输出:它甚至没有打印XML中的所有等级,即跳过了67846464,因此该等级也将被打印在 output.xml
中,尽管它不在列表中
console output: It is not even printing all the ranks from XML i.e. 67846464 is skipped so this rank will also be printed in the output.xml
though it is not there in the list
root is <Element 'data' at 0x7f5929a9d8b0>
<class 'xml.etree.ElementTree.Element'>
123456
789045
当前输出:具有2个ID 123456和67846464
Current output: having 2 ids 123456 and 67846464
<data>
<continents>
<country>
<state>
<rank updated="yes">123456</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</state>
<zones>
<pretty>yes</pretty>
</zones>
</country>
<country>
<state>
<rank updated="yes">67846464</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<gpc>59900</gpc>
<neighbor name="Malaysia" direction="N" />
</state>
<zones>
<pretty>No</pretty>
</zones>
<market>
<pretty>cool</pretty>
</market>
</country>
</continents>
</data>
预期输出:由于列表中没有67846464,因此只能输出123456
Expected output: only 123456 should come as 67846464 is not in the list
<data>
<continents>
<country>
<state>
<rank updated="yes">123456</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</state>
<zones>
<pretty>yes</pretty>
</zones>
</country>
</continents>
</data>
推荐答案
此处的问题是 root
元素中不包含国家/地区.它包含在 continents
标记中.解决该问题的一种方法是遍历 root
中的 continents
,然后检查 country
的 rank
.可以使用以下代码完成此操作:
The problem here is that country is not contained in root
element. It is contained in the continents
tag. One solution to the problem is to iterate over continents
in root
and then check for rank
of country
. This can be done using the following code:
import xml.etree.ElementTree as ET
tree = ET.parse('tes.xml')
lis = ['2', '5']
root = tree.getroot()
print('root is', root)
print(type(root))
for continent in root.findall('.//continents'):
for country in continent:
rank = country.find('state/rank').text
print(rank)
if rank not in lis:
continent.remove(country)
tree.write('outpu.xml')
修改
我们不能简单地使用来遍历各大洲的国家
We can't simply iterate over countries in continents using
用于大陆国家/地区
因为大洲是< class'xml.etree.ElementTree.Element'>
,要对其进行迭代,我们必须使用 findall
.更新后的代码如下:
for country in continent
because continent is <class 'xml.etree.ElementTree.Element'>
and to iterate over it we must use findall
. The updated code is as follows:
import xml.etree.ElementTree as ET
tree = ET.parse('tes.xml')
lis = ['123456']
root = tree.getroot()
print('root is', root)
print(type(root))
for continent in root.findall('.//continents'):
for country in continent.findall('.//country'):
rank = country.find('state/rank').text
print(rank)
if rank not in lis:
print('country is', country)
print(country in continent)
continent.remove(country)
tree.write('outpu.xml')
这篇关于由于出现错误"ValueError:list.remove(x):x not in list"而无法从XML提取元素;在python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!