为什么 ElementTree.iterparse() 会引发 ParseError?
import xml.etree.ElementTree as ET
xmldata = file('my_xml_file.xml')
tree = ET.parse(xmldata)
root = tree.getroot()
root_iter = root.iter()
现在我可以调用 root_iter.next()
并获取我的 Element
对象。问题是我正在使用的实际文件很大,我无法将其全部放入内存中。所以我尝试使用:
parse_iter = ET.iterparse(xmldata)
如果我调用 parse_iter.next()
它会引发以下问题
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
parse_iter.next()
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1260, in next
self._root = self._parser.close()
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1636, in close
self._raiseerror(v)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1488, in _raiseerror
raise err
ParseError: no element found: line 1, column 0
我做错了什么?
import xml.etree.ElementTree as ET
xmldata = file('my_xml_file.xml')
tree = ET.parse(xmldata)
root = tree.getroot()
root_iter = root.iter()
Now I can call root_iter.next()
and get my Element
objects. The problem is the real file I am working with is huge and I can't fit all of it in memory. So I am trying to use:
parse_iter = ET.iterparse(xmldata)
If I call parse_iter.next()
it raises the following
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
parse_iter.next()
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1260, in next
self._root = self._parser.close()
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1636, in close
self._raiseerror(v)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1488, in _raiseerror
raise err
ParseError: no element found: line 1, column 0
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的代码非常好,除了我在已经使用
ElementTree.parse()
读取的文件对象上调用ElementTree.iterparse()
。噢!因此,对于那些碰巧犯同样错误的人,解决方案是打开一个新的文件对象或使用
file.seek(0)
重置文件光标。The code I had was perfectly fine, except I was calling
ElementTree.iterparse()
on a file object I had already read withElementTree.parse()
. D'oh!So for those who happen to make the same mistake, the solution is to either open a new file object or use
file.seek(0)
to reset the file cursor.