ElementTree: list.remove(x): x 不在列表中

ElementTree: list.remove(x): x not in list

我有 xml 包裹在字符串中的数据,xml 数据如下所示:

<Root>
   <Header>
      <information>info</information>
   </Header>
   <Main>
      <Product>
         <Name>name1</Name>
         <Description>description1</Description>
      </Product>
      <Product>
         <Name>name2</Name>
         <Description>description2</Description>
      </Product>
   </Main>
</Root>

我想从中删除 Header 标签

tree = ET.fromstring(xml_string)
tree.remove(tree.findall('.//Header'))

但出现错误list.remove(x): x not in list。我做错了什么。谢谢

tree.remove 接受单个元素,而不是元素列表,这就是 tree.findall returns。您可以为找到的每个 header 调用 tree.remove,或者如果只有一个 Header.[=,则只使用 tree.find 而不是 tree.findall 16=]