【问题标题】:Element does not exist when I want to remove it当我想删除它时元素不存在
【发布时间】:2020-06-08 19:18:42
【问题描述】:
<?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 个。

在所有这些孩子中,我想删除那些带有&lt;Livre&gt;0&lt;/Livre&gt; 的孩子,总共代表 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")

【问题讨论】:

  • 如果 XML 只包含一个孩子,这是一个有问题的孩子,它可以工作吗?
  • 可能在迭代时从root 中删除元素会打乱迭代 - 您可以在删除每个子项后尝试重新启动整个 for 循环吗?现在可以吗?
  • 或者更改for 语句,使其遍历列表,我认为for child in list(root): 应该可以工作。
  • 我认为root 是一个迭代实时 XML 的生成器,我猜它不喜欢在迭代时删除元素。使用 list(root) 从生成器创建一个简单的列表。
  • 没有必要像其他网站那样在标题中加上 [RESOLVED] 或 [SOLVED]。在 Stack Overflow 中,已接受答案上的绿色复选标记和 green box in the questions list 已经表明了这一点。标题中也不需要打标签,见section on Should I use tags in titles?

标签: python xml loops python-3.7 elementtree


【解决方案1】:

像这样更改 for 循环,这样它就可以遍历一个列表,然后删除单个元素就不会感到困惑。

for child in list(root):
    if child.find('Livre').text != "1":
        root.remove(child)

【讨论】:

    猜你喜欢
    • 2021-05-06
    • 1970-01-01
    • 2015-10-21
    • 2021-06-22
    • 2019-04-03
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    相关资源
    最近更新 更多