[英]lxml pretty_print python memory overload
I have a poorly formatted xml file with over 350 MB of data. 我有一个格式不佳的xml文件,有超过350 MB的数据。 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)
我的电脑有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()
You might want to try using the write method directly with the file handle rather than calling tostring. 您可能希望尝试直接使用write方法使用文件句柄而不是调用tostring。 Change this line:
改变这一行:
f.write(etree.tostring(tree,pretty_print=True))
to this: 对此:
tree.write(f, pretty_print=True)
This should hopefully reduce the memory usage by half. 这应该有望减少一半的内存使用量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.