使用python从XML中仅获取所需元素时,获取错误“list.remove(x):x不在列表中”

2024-05-29 12:01:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个代码,从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>

Tags: nameindatarootxmlyearcountryyes
1条回答
网友
1楼 · 发布于 2024-05-29 12:01:11

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)

相关问题 更多 >

    热门问题