Iterparse对象接下来没有属性

ADW*_*LSH 4 python memory celementtree iterparse

我正在解析700mb文件,我有以下代码在我的测试文件上工作正常,没有行context.iter(context)和事件,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()
Run Code Online (Sandbox Code Playgroud)

然而,当我继续iterparse 700mb文件它仍然崩溃.阅读:

清除()树时使用ElementTree.iterparse()时内存使用量是否会增加?

我相信这是因为我需要清除root,但是我已经遵循了上面已经包含的一些教程我不断收到错误:

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

我还是Python的新手,我无法理解为什么会发生这种情况,据我所知,我与这些例子一致.

hor*_*rns 12

假设您使用的是Python3,则迭代器语法next(context)代替context.next()