【问题标题】:Memory error in Python while parsing a 300 MB file解析 300 MB 文件时 Python 中的内存错误
【发布时间】:2016-04-04 11:40:57
【问题描述】:

我正在用 python 3.5 解析一个 xml 文件(291 MB)

import xmltodict, json

with open('Wikipedia-20160404094133.xml', encoding='utf-8') as xml_file:
    dic_xml = xmltodict.parse(xml_file.read(), encoding='utf-8', xml_attribs=True)

但我得到了错误:

dic_xml = xmltodict.parse(xml_file.read(), encoding='utf-8', xml_attribs=True)
MemoryError

我能做些什么来解决这个问题?

【问题讨论】:

  • 在任何相对现代的系统上,我都不认为这会导致内存错误 - 您是在共享服务器上运行还是在 RAM 有限的云上运行?
  • 您需要提供有关您正在运行的系统和您的 python 安装的更多信息。

标签: python parsing memory


【解决方案1】:

查看this

"xmltodict 非常快(基于 Expat)并且具有内存占用少的流模式,适用于大型 XML 转储,如 Discogs 或 维基百科"

本质上,您需要分块读取文件,而 xmltodict 的“流模式”似乎就是为此而构建的。

【讨论】:

    猜你喜欢
    • 2012-11-09
    • 2021-05-21
    • 1970-01-01
    • 2021-11-16
    • 2013-06-19
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多