Iterparse object has no attribute next
Asked Answered
F

1

5

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.

Fp answered 16/2, 2015 at 13:27 Comment(1)
You're missing the initial context = iterparse() call.Lightless
A
15

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

Arleta answered 16/2, 2015 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.