[英]xml tree remove elements
I am trying to remove elements from my tree based on the elements attributes.我正在尝试根据元素属性从我的树中删除元素。
import xml.etree.cElementTree as ET
import requests
xml = requests.get("https://iptv-org.github.io/epg/guides/ca/sportsnet.ca.epg.xml").text
tree = ET.ElementTree(ET.fromstring(xml))
root = tree.getroot()
for elem in tree.iter():
if elem.tag == "channel" or elem.tag == "programme":
if elem.attrib.get("id", "") == "WWENetworkCanada.us" or elem.attrib.get("channel", "") == "WWENetworkCanada.us":
pass
else:
print("removing")
root.remove(elem)
ET.dump(root)
The dump still has the elements I am trying to remove, even though I see "removing".转储仍然有我要删除的元素,即使我看到“正在删除”。 Can anyone see why?
谁能明白为什么?
I am expecting everything that is not "WWENetworkCanada.us" to be removed.我希望删除所有不是“WWENetworkCanada.us”的内容。
Thanks, Chris谢谢,克里斯
It was failing because I was changing indexes as I iterating and removing.它失败了,因为我在迭代和删除时正在更改索引。 What I needed to do was
for elem in list(tree.iter()):
.我需要做的是
for elem in list(tree.iter()):
。 This is something I learnt some time ago (on here) that lets you remove while iterating without creating a "temp" list.这是我前段时间(在这里)学到的东西,它可以让你在迭代时删除而不创建“临时”列表。
Saw you answered your own question, but thought I'd share as an alternate approach.看到你回答了你自己的问题,但我认为我会分享作为替代方法。 Simply use
findall()
to find and remove any values that don't match what you are looking for只需使用
findall()
查找并删除与您要查找的内容不匹配的任何值
for elem in tree.findall("./channel/[@id!='WWENetworkCanada.us']"):
root.remove(elem)
for elem in tree.findall("./programme/[@channel!='WWENetworkCanada.us']"):
root.remove(elem)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.