【问题标题】:MemoryError with minidom in PythonPython中带有minidom的MemoryError
【发布时间】:2012-06-20 20:18:48
【问题描述】:

我在 Python 中有一个带有 minidom 解析器的 MemoryError。 我正在读取 8000 个小文件(大多数小于 50 Kb),在读取 2500 次后出现此错误...`

Traceback (most recent call last): 

 File "C:\eclipse\plugins\org.python.pydev.debug_2.4.0.2012020116\pysrc\pydevd.py", line 1307, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\eclipse\plugins\org.python.pydev.debug_2.4.0.2012020116\pysrc\pydevd.py", line 1060, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "C:\Users\calculator_2012.py", line 81, in <module>
    file_content, economicFlow, elementaryFlow = XML_reader(spoldFile)
  File "C:\Users\XML_reader.py", line 10, in XML_reader
    xmltree = parse(spold_filename)
  File "C:\Python27\lib\xml\dom\minidom.py", line 1914, in parse
    return expatbuilder.parse(file)
  File "C:\Python27\lib\xml\dom\expatbuilder.py", line 924, in parse
    result = builder.parseFile(fp)
  File "C:\Python27\lib\xml\dom\expatbuilder.py", line 207, in parseFile
    parser.Parse(buffer, 0)
  File "C:\Python27\lib\xml\dom\expatbuilder.py", line 294, in character_data_handler_cdata
    _append_child(self.curNode, node)
  File "C:\Python27\lib\xml\dom\minidom.py", line 274, in _append_child
    def _append_child(self, node):
  File "C:\eclipse\plugins\org.python.pydev.debug_2.4.0.2012020116\pysrc\pydevd.py", line 942, in trace_dispatch
    traceback.print_exc()
  File "C:\Python27\lib\traceback.py", line 232, in print_exc
    print_exception(etype, value, tb, limit, file)
MemoryError

有没有人可以建议“无内存泄漏”解析器?

【问题讨论】:

  • 试试xml.etree.cElementTree。如果你在 Eclipse 之外运行你的脚本,你会得到 MemoryError 吗?

标签: python minidom


【解决方案1】:

我还建议使用内置的 cElementTree。 Minidom 有很多问题:/

不然lxml也挺好的,功能也比较多。

【讨论】:

    【解决方案2】:

    我似乎 minidom 会占用很多内存。

    我尝试解析一个56MB的文件,读取它需要8G内存。

    你算算..

    【讨论】:

      【解决方案3】:

      libxml2 速度更快,没有内存泄漏,但它太 C 了,没有任何好处,而且文档很糟糕

      lxml 是 libxm2 和 etree 模块之上的一层,因此这是具有 libxml2 性能的 python 方式

      http://lxml.de/

      编辑:错别字

      【讨论】:

        【解决方案4】:

        嗯,回溯表明您在 PyDev 调试器中运行,那么,您是否尝试在没有调试器的情况下在 Eclipse 中运行它?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-10-22
          • 1970-01-01
          • 2015-03-20
          • 2019-04-10
          • 1970-01-01
          • 1970-01-01
          • 2011-04-28
          相关资源
          最近更新 更多