Search code examples
pythonmemoryiterparsecelementtree

Iterparse object has no attribute next


I am parsing a 700mb file, I have the following code with works fine on my test file without the line context.iter(context) and event, elem = context.next().

form xml.etree import cElementTree as ET
source = ("AAT.xml")
context = iter(context)
event, root = context.next()
for event, elem in context:
         if event == event("end"):
             some processing...
             elem.clear()
         root.clear()

However when I move on to iterparse the 700mb the file it still crashes out. Having read:

Should memory usage increase when using ElementTree.iterparse() when clear()ing trees?

I believe this is because I need to clear the root, however having followed a few tutorials which I have incorporated above I'm continually getting an error:

line 9, in <module>
event, root = context.next()
AttributeError: 'iterparse' object has no attribute 'next'

I'm still new to Python and I cannot work out why this is happening, as far as I can see I am in line with the examples.


Solution

  • Assuming you are using Python3, the iterator syntax is next(context) instead of context.next()