在我的问题面前,我有点绝望了。
<?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>
例如,那些节点,比如说while留下。
这是我的代码。
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)