lxml.etree._Element.append() 来自循环未按预期工作

lxml.etree._Element.append() from a loop not working as expected

我想知道为什么在此代码中 append() 似乎在循环内部工作,但结果 xml 仅显示最后一次迭代的修改,而 remove()按预期工作。这是一个过于简化的示例,我正在处理大块数据,并且需要将相同的子树附加到许多不同的父级。

from lxml import etree

xml = etree.fromstring('<tree><fruit id="1"></fruit><fruit id="2"></fruit></tree>')
sub = etree.fromstring('<apple/>')

for i, item in enumerate(xml):
    item.append(sub)
    print('Fruit {} with sub appended: {}'.format(
        i, etree.tostring(item).decode('ascii')))

print('\nResulting tree after iterating through items with append():\n' +
    etree.tostring(xml, pretty_print=True).decode('ascii'))

for item in xml:
    xml.remove(item)

print('Resulting tree after iterating through items with remove():\n' +
    etree.tostring(xml, pretty_print=True).decode('ascii'))

当前输出:

Fruit 0 with sub appended: <fruit id="1"><apple/></fruit>
Fruit 1 with sub appended: <fruit id="2"><apple/></fruit>

Resulting tree after iterating through items with append():
<tree>
  <fruit id="1"/>
  <fruit id="2">
    <apple/>
  </fruit>
</tree>

Resulting tree after iterating through items with remove():
<tree/>

使用 append() 遍历项目后 的预期输出:

<tree>
  <fruit id="1"/>
    <apple/>
  </fruit>
  <fruit id="2">
    <apple/>
  </fruit>
</tree>

那是因为您只创建了 一个 <apple/> 的实例 以供追加。所以基本上你只是将一个实例从一个父级移动到另一个父级,直到最后一个 append(sub) 被执行。尝试在 for 循环中移动 <apple/> 元素的创建:

for i, item in enumerate(xml):
    sub = etree.fromstring('<apple/>')
    item.append(sub)
    print('Fruit {} with sub appended: {}'.format(
        i, etree.tostring(item).decode('ascii')))
print()

输出:

Resulting tree after iterating through items with append():
<tree>
  <fruit id="1">
    <apple/>
  </fruit>
  <fruit id="2">
    <apple/>
  </fruit>
</tree>