简体   繁体   English

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

[英]ElementTree: list.remove(x): x not in list

I have xml data that wrapped in string, xml data look like this: 我有包裹在字符串中的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>

I want delete Header tag from it 我想从中删除Header标签

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

But it rise error list.remove(x): x not in list . 但是它会出现错误list.remove(x): x not in list What i did wrong. 我做错了。 Thank you 谢谢

tree.remove takes a single element, not a list of elements, which is what tree.findall returns. tree.remove接受单个元素,而不是元素列表,这是tree.findall返回的内容。 You can either call tree.remove for every header you find, or just use tree.find instead of tree.findall if there's only ever going to be one Header . 您可以拨打tree.remove为每次找头,或者只是使用tree.find代替tree.findall如果只有永远将是一个Header

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM