如何使用lxml在python中删除与xpath匹配的所有元素?

2024-09-28 20:44:47 发布

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

所以我有这样的XML:

<bar>
  <foo>Something</foo>
  <baz>
     <foo>Hello</foo>
     <zap>Another</zap>
  <baz>
<bar>

我想删除所有的foo节点。这样不行

params = xml.xpath('//foo')
for n in params:
  xml.getroot().remove(n)

给予

ValueError: Element is not a child of this node.

做这件事的好方法是什么?


Tags: inhellofor节点fooanotherbarxml