我无法使用xml.etree.ElementTree文件. 我发现了类似的情况here,但这并不能解决我的问题。我也读过ElementTree和XPath的文档。你知道吗
我有一个类似这样的xml树
<metadata>
<lineage>
<srcinfo></srcinfo>
<procstep>
<otherinfo></otherinfo>
</procstep>
<procstep>
<otherinfo></otherinfo>
</procstep>
<procstep>
<otherinfo></otherinfo>
</procstep>
<procstep>
<otherinfo></otherinfo>
</procstep>
</lineage>
</metadata>
假设我想删除第二、第三和第四步元素。我尝试了以下代码,结果是“ValueError:列表.删除(x) :x not in list“错误。你知道吗
while len(root.findall('.//lineage/procstep')) > 1:
root.remove(root.findall('.//lineage/procstep[last()]'))
有没有什么建议来解释为什么这样做行不通?有没有其他办法解决我的问题?提前谢谢你的建议。你知道吗
删除最后一步
要删除
procstep
元素,请使用procstep的父级(lineage
)。你知道吗尝试:
如果使用lxml,则可以使用
getparent()
,如下所示:删除procstep元素,但删除第一个
相关问题 更多 >
编程相关推荐