我有包裹在字符串中的 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
返回的元素列表。您可以为找到的每个 header 调用 tree.remove
,或者仅使用 tree.find
而不是 tree.findall
(如果只存在)成为一个标题
。
关于python - ElementTree: list.remove(x): x 不在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34357307/