繁体   English   中英

使用lxml python从xml文件中删除元素

[英]remove element from xml file with lxml python

我试图从大xml文件中删除特定条目。

我从应删除的文本输入列表中通过其文本找到了特定条目。

我运行此代码:

#!/usr/bin/env python

from lxml import etree

g = open("/root/simplexml.xml", "rw")
f = etree.parse(g)

listdown = ["http://aiddp.org/administrator/components/com_attachments/controllers/Global%20Service/86af744091ea22ad5b1372ac7978b51f","http://primepromap.com/es/wp-includes/css/survey/survey/index.php?randInboxLightaspxn.17http://primepromap.com/es/wp-includes/css/survey/survey/index.php?randInboxLightaspxn.1774256418http:/peelrealest.com/property/ihttp://www.nwolb.com.default.aspx.refererident.568265843.puntopatrones.cl/wp-admin/js/upgrade/upgrade1.zip-extracted/upgrade/newp/loading.php="]

for downsite in listdown:
    for found in f.xpath(".//url[text()='"+downsite+"']"):
        print "deleted "+str(found)
        found.getparent().remove(found)

print "over"

它应该可以工作,但是在我打开xml文件之后,应该删除的肠仍然存在...这里是什么问题?

您需要将修改后的树转储回xml文件

f.write("/root/simplexml.xml")

暂无
暂无

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

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