[英]Element does not exist when I want to remove it
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<?xml-stylesheet href="Famille.xsl" type="text/xsl"?><HF_DOCUMENT>
<Data>
<CodeFamille>12 BIS</CodeFamille>
<Libelle>12 bis</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>3700744900020</CodeFamille>
<Libelle>BALLON GONFLABLE</Libelle>
<Livre>0</Livre>
</Data>
<Data>
<CodeFamille>4634510541685140</CodeFamille>
<Libelle>AKATA</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>707003</CodeFamille>
<Libelle>Hacomo</Libelle>
<Livre>0</Livre>
</Data>
<Data>
<CodeFamille>9782756076430</CodeFamille>
<Libelle>Draw tome 2</Libelle>
<Livre>0</Livre>
</Data>
<Data>
<CodeFamille>ACCESSOIRE</CodeFamille>
<Libelle>Figurine</Libelle>
<Livre>0</Livre>
</Data>
<Data>
<CodeFamille>AKIKO</CodeFamille>
<Libelle>Akiko</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>AKILEOS</CodeFamille>
<Libelle>Akileos</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>AKUMA</CodeFamille>
<Libelle>Akuma</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ALBIN MICHEL</CodeFamille>
<Libelle>Albin Michel</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ALIMENTATION</CodeFamille>
<Libelle>Alimentation 5.5</Libelle>
<Livre>0</Livre>
</Data>
<Data>
<CodeFamille>AMALTHEE</CodeFamille>
<Libelle>Amalthee</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ANIME MANGA PRESSE</CodeFamille>
<Libelle>Anime Manga Presse</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ANKAMA</CodeFamille>
<Libelle>Ankama</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ARTEFAC</CodeFamille>
<Libelle>Artefac</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ASIAN DISTRICT</CodeFamille>
<Libelle>Asian district</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ASSIMIL</CodeFamille>
<Libelle>Assimil</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ASUKA</CodeFamille>
<Libelle>ASUKA</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ATOMIC CLUB</CodeFamille>
<Libelle>Atomic club</Libelle>
<Livre>1</Livre>
</Data>
<Data>
<CodeFamille>ATRABILE</CodeFamille>
<Libelle>Atrabile</Libelle>
<Livre>1</Livre>
</Data>
...
</HF_DOCUMENT>
当我运行一个简单的循环来查看 XML 文件时,我可以找到每个孩子,总共 203 个。
在所有这些孩子中,我想删除那些<Livre>0</Livre>
总共代表 63 个的孩子。
我的问题是,当我用一个简单的条件运行相同的循环以仅删除那些尊重条件的人时,我无法删除所有这些,只有 43 个被删除。
<Data>
<CodeFamille>9782756076430</CodeFamille>
<Libelle>Draw tome 2</Libelle>
<Livre>0</Livre>
</Data>
...
<Data>
<CodeFamille>ALIMENTATION</CodeFamille>
<Libelle>Alimentation 5.5</Libelle>
<Livre>0</Livre>
</Data>
例如,这些节点在停留时。
这是我的代码:
tree = ET.parse("test.xml")
root = tree.getroot()
for child in root:
if child.find('Livre').text != "1":
root.remove(child)
tree.write("output.xml")
像这样更改 for 循环,使其遍历列表,然后删除单个元素不会感到困惑。
for child in list(root):
if child.find('Livre').text != "1":
root.remove(child)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.