Hello,

While following the iterparse / modifying the tree docs 
(https://lxml.de/4.5/parsing.html#modifying-the-tree) in attempt to clean up 
preceding siblings, code raises a TypeError: 'NoneType' object does not support 
item deletion when the XML contains a comment preceding the root element. 
Please advise on a workaround for this use case if this is not a potential bug. 
See below reproducible example. Thank you for your time and review.

```python
from io import BytesIO
import lxml.etree as ET

xml_txt = '''\
<!-- it will choke on this comment -->
<issue>
  <type>BUG</type>
</issue>
'''.encode('utf-8')

iterparse_columns = {
    "issue": ["type"]
}

data =  []
for event, elem in ET.iterparse(BytesIO(xml_txt), events=('start', 'end')):
    node = next(iter(iterparse_columns))
    curr_elem = (
        elem.tag.split('}')[1] if '}' in elem.tag else elem.tag
    )

    if event == 'start':
        if curr_elem == node:
            row = {}

        for col in iterparse_columns[node]:
            if curr_elem == col:
                row[col] = (
                    elem.text.strip() 
                    if elem.text is not None 
                    else elem.text
                )
            if col in elem.attrib:
                row[col] = elem.attrib[col].strip()

    if event == 'end':
        if curr_elem == node:
            data.append(row)

    elem.clear()
    while elem.getprevious() is not None:
        del elem.getparent()[0]

print(data)
```

Full traceback:

```
Traceback (most recent call last):
  File "iterparse_parent_comment_reprex.py", line 43, in <module>
    del elem.getparent()[0]
TypeError: 'NoneType' object does not support item deletion
```
_______________________________________________
lxml - The Python XML Toolkit mailing list -- lxml@python.org
To unsubscribe send an email to lxml-le...@python.org
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: arch...@mail-archive.com

Reply via email to