当我想删除它时元素不存在
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 文件时,我可以找到每个 children,总共 203 个。
在所有这些children中,我想删除那些<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>
那些节点例如 while stay.
这是我的代码:
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)
<?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 文件时,我可以找到每个 children,总共 203 个。
在所有这些children中,我想删除那些<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>
那些节点例如 while stay.
这是我的代码:
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)