python – xml.etree.ElementTree iterparse()仍然使用大量内存?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – xml.etree.ElementTree iterparse()仍然使用大量内存?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2356字,纯文字阅读大概需要4分钟。
内容图文
我一直在尝试使用iterparse来减少需要处理大型XML文档的脚本的内存占用量.这是一个例子.我编写了这个简单的脚本来读取TMX文件并将其拆分为一个或多个输出文件,不超过用户指定的大小.尽管使用iterparse,当我将886MB文件拆分为100MB文件时,脚本会以所有可用内存运行(使用我的8MB中的6.5进行爬行).
难道我做错了什么?为什么内存使用量如此之高?
#! /usr/bin/python
# -*- coding: utf-8 -*-
import argparse
import codecs
from xml.etree.ElementTree import iterparse, tostring
from sys import getsizeof
def startNewOutfile(infile, i, root, header):
out = open(infile.replace('tmx', str(i) + '.tmx'), 'w')
print >>out, '<?xml version="1.0" encoding="UTF-8"?>'
print >>out, '<!DOCTYPE tmx SYSTEM "tmx14.dtd">'
print >>out, roottxt
print >>out, headertxt
print >>out, '<body>'
return out
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--maxsize', dest='maxsize', required=True, type=float, help='max size (in MB) of output files')
parser.add_argument(dest='infile', help='.tmx file to be split')
args = parser.parse_args()
maxsize = args.maxsize * 1024 * 1024
nodes = iter(iterparse(args.infile, events=['start','end']))
_, root = next(nodes)
_, header = next(nodes)
roottxt = tostring(root).strip()
headertxt = tostring(header).strip()
i = 1
curr_size = getsizeof(roottxt) + getsizeof(headertxt)
out = startNewOutfile(args.infile, i, roottxt, headertxt)
for event, node in nodes:
if event =='end' and node.tag == 'tu':
nodetxt = tostring(node, encoding='utf-8').strip()
curr_size += getsizeof(nodetxt)
print >>out, nodetxt
if curr_size > maxsize:
curr_size = getsizeof(roottxt) + getsizeof(headertxt)
print >>out, '</body>'
print >>out, '</tmx>'
out.close()
i += 1
out = startNewOutfile(args.infile, i, roottxt, headertxt)
root.clear()
print >>out, '</body>'
print >>out, '</tmx>'
out.close()
解决方法:
在相关问题中找到答案:Why is elementtree.ElementTree.iterparse using so much memory?
在for循环的每次迭代中,不仅需要root.clear(),还需要node.clear().因为我们正在处理启动和启动但是,我们需要注意不要过早删除tu节点:
for e, node in nodes:
if e == 'end' and node.tag == 'tu':
nodetxt = tostring(node, encoding='utf-8').strip()
curr_size += getsizeof(nodetxt)
print >>out, nodetxt
node.clear()
if curr_size > maxsize:
curr_size = getsizeof(roottxt) + getsizeof(headertxt)
print >>out, '</body>'
print >>out, '</tmx>'
out.close()
i += 1
out = startNewOutfile(args.infile, i, roottxt, headertxt)
root.clear()
内容总结
以上是互联网集市为您收集整理的python – xml.etree.ElementTree iterparse()仍然使用大量内存?全部内容,希望文章能够帮你解决python – xml.etree.ElementTree iterparse()仍然使用大量内存?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。