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

时间:2016-03-25 00:14:01

标签: python xml lxml

我试图从大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文件之后,应该删除的enteries仍然存在... 这有什么问题?

1 个答案:

答案 0 :(得分:1)

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

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