lxml Pretty_print python 内存过载
我有一个格式不正确的 xml 文件,其中包含超过 350 MB 的数据。基本上,所有数据都合并到一行中。我正在尝试将其漂亮打印到一个新文件中以使生活更轻松,但遇到了内存问题。我在这里做错了什么吗?有办法解决这个问题吗?我的电脑有 4GB RAM,是四核 i5-2410M (2.30Ghz)
import os
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse('filename',parser)
f = open('filename',"w")
f.write(etree.tostring(tree,pretty_print=True))
f.close()
I have a poorly formatted xml file with over 350 MB of data. Basically, all the data was consolidated into one line. I am trying to pretty_print this into a new file to make life easier, but am running into memory issues. Am I doing anything wrong here and is there a way around this? My computer has 4GB of RAM and is a Quad-Core i5-2410M (2.30Ghz)
import os
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse('filename',parser)
f = open('filename',"w")
f.write(etree.tostring(tree,pretty_print=True))
f.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想尝试直接使用文件句柄的 write 方法,而不是调用 tostring。将此行: 更改
为:
这有望将内存使用量减少一半。
You might want to try using the write method directly with the file handle rather than calling tostring. Change this line:
to this:
This should hopefully reduce the memory usage by half.