我正在尝试使用 xml.etree 在 python 中读取 XML 文件,但有时对于某些文件,我在解析文件时会出现内存错误。我的 XML 文件大小为 912Mb,问题与文件大小有关吗?
代码:
from xml.etree import ElementTree
with open('F:\\Reports\\Logs\\AppPerfect_States\\TG1_GM\\Result_TG1_V16.xml', 'rt') as f1:
tree = ElementTree.parse(f1)
错误:
Traceback (most recent call last):
File "<pyshell#3>", line 2, in <module>
tree = ElementTree.parse(f1)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1182, in parse
tree.parse(source, parser)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 653, in parse
data = source.read(65536)
MemoryError
更新:根据许多建议,我尝试了 lxml
代码:
from lxml import etree
context = etree.iterparse('F:\\Reports\\Logs\\AppPerfect_States\\TG1_GM\\Result_TG1_V16.xml',tag = "document")
for event, element in context:
for child in element:
print child.tag, child.text
element.clear()
错误:
C:\Python27\python.exe "F:/Py Projects/V16_AUTO/test1/xmlparsingtest1.py"
Traceback (most recent call last):
File "F:/Py Projects/V16_AUTO/test1/xmlparsingtest1.py", line 3, in <module>
for event, element in context:
File "iterparse.pxi", line 207, in lxml.etree.iterparse.__next__ (src\lxml\lxml.etree.c:126137)
lxml.etree.XMLSyntaxError: unknown error, line 7530730, column 33
更新 2:尝试了 cElementTree
代码:
import xml.etree.cElementTree as etree
xmL = 'F:\\Reports\\Logs\\Result_TG1_V16.xml'
context = etree.iterparse(xmL, events=("start", "end"))
context = iter(context)
event, root = context.next()
for event, elem in context:
if event == 'TasksReportNode':
print elem.tag
print elem.text
root.clear()
错误:
Exception MemoryError: in ignored
Exception MemoryError: in ignored
Exception MemoryError: in ignored
Exception MemoryError: in ignored
Exception MemoryError: in ignored
MemoryError