Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have xml data that wrapped in string, xml data look like this:

<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

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

But it rise error list.remove(x): x not in list. What i did wrong. Thank you

like image 867
Zagorodniy Olexiy Avatar asked Nov 24 '25 14:11

Zagorodniy Olexiy


1 Answers

tree.remove takes a single element, not a list of elements, which is what tree.findall returns. 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.

like image 148
Wander Nauta Avatar answered Nov 27 '25 02:11

Wander Nauta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!