lxml - minor problem appending new element

Frank Millman frank at chagford.com
Mon Feb 3 01:56:50 EST 2020


Hi all

I usually send lxml queries to the lxml mailing list, but it appears to 
be not working, so I thought I would try here.

This is a minor issue, and I have found an ugly workaround, but I 
thought I would mention it.

In Python I can iterate through a list, and on a certain condition 
append a new item to the list, which is then included in the iteration.

 >>> x = ['a', 'b', 'c']
 >>> for y in x:
...   print(y)
...   if y == 'b':
...     x.append('d')
...
a
b
c
d
 >>> x
['a', 'b', 'c', 'd']
 >>>

The same thing works in lxml -

 >>> lmx = '<x><y z="a"/><y z="b"/><y z="c"/></x>'
 >>> xml = etree.fromstring(lmx)
 >>> for y in xml:
...   print(etree.tostring(y))
...   if y.get('z') == 'b':
...     xml.append(etree.Element('y', attrib={'z': 'd'}))
...
b'<y z="a"/>'
b'<y z="b"/>'
b'<y z="c"/>'
b'<y z="d"/>'
 >>> etree.tostring(xml)
b'<x><y z="a"/><y z="b"/><y z="c"/><y z="d"/></x>'

However, if it happens that the condition is met on the last item in the 
list, Python still works, but lxml does not include the appended item in 
the iteration. In the following, the only change is checking for 'c' 
instead of 'b'.

 >>> x = ['a', 'b', 'c']
 >>> for y in x:
...   print(y)
...   if y == 'c':
...     x.append('d')
...
a
b
c
d
 >>> x
['a', 'b', 'c', 'd']
 >>>

 >>> lmx = '<x><y z="a"/><y z="b"/><y z="c"/></x>'
 >>> xml = etree.fromstring(lmx)
 >>> for y in xml:
...   print(etree.tostring(y))
...   if y.get('z') == 'c':
...     xml.append(etree.Element('y', attrib={'z': 'd'}))
...
b'<y z="a"/>'
b'<y z="b"/>'
b'<y z="c"/>'
 >>> etree.tostring(xml)
b'<x><y z="a"/><y z="b"/><y z="c"/><y z="d"/></x>'

As you can see, the last element is correctly appended, but is not 
included in the iteration.

Is there any chance that this can be looked at, or is it just the way it 
works?

BTW, I see that ElementTree in the standard library does not have this 
problem.

Thanks

Frank Millman



More information about the Python-list mailing list