Unable to fetch elements from XML as getting error "ValueError: list.remove(x): x not in list" in 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:

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 中,尽管它不在列表中

root is <Element 'data' at 0x7f5929a9d8b0>
<class 'xml.etree.ElementTree.Element'>
123456
789045

当前输出:有 2 个 ID 123456 和 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>

预期输出:只有 123456 应该出现,因为 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>
  </continents>  
</data>

这里的问题是国家/地区未包含在 root 元素中。它包含在 continents 标签中。该问题的一种解决方案是在 root 中迭代 continents,然后检查 country 中的 rank。这可以使用以下代码完成:

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')

编辑

我们不能简单地使用

遍历各大洲的国家

for country in continent
因为 continent 是 <class 'xml.etree.ElementTree.Element'> 并且要迭代它我们必须使用 findall。更新后的代码如下:

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')