我正在使用 lxml etree.parse 以某种方式解析一个巨大的 XML 文件(大约 65MB - 300MB)。当我运行包含以下函数的独立 python 脚本时,我遇到内存分配失败:
Error:
Memory allocation failed : xmlSAX2Characters, line 5350155, column 16
部分功能代码:
def getID():
try:
from lxml import etree
xml = etree.parse(<xml_file>) # here is where the failure occurs
for element in xml.iter():
...
result = <formed by concatenating element texts>
return result
except Exception, ex:
<handle exception>
奇怪的是,当我在 IDLE 上输入相同的函数并测试相同的 XML 文件时,我没有遇到任何 MemoryAllocation 错误。
关于这个问题的任何想法?提前致谢。