Generating very large XML files in Python? -


Does anyone know memory efficient ways to generate very large XML files (eg 100-500 MiB) in Python? is?

I am using lxml , but the memory usage is through the roof.

Maybe you can use the templing engine instead of creating / creating XML?

For example, XML-based and supports streaming output. A very basic example: genshi.template import markup template from

  tpl_xml = '' '& lt; Doc xmlns: py = "http://genshi.edgewall.org/" & gt; & Lt; P py: for = "I am in data" & gt; $ {I} & lt; / P & gt; & Lt; / Doc & gt; F: stream.render (out = f) as' open '(' output.xml ',' w ') with' '' tpl = markupTemplate (tpl_xml) stream = tpl.generate (data = xrange (10000000)) < / Code> 

It may take a little while, but memory usage is low.

Not for the templateing engine (for example, "natively" xml), but very fast:

From Mako.template import contact context by mako.runtime import tpl_xml = '' '& Lt; Doc & gt; % I for data: & lt; P & gt; $ {I} & lt; / P & gt; % Endfor & lt; / Doc & gt; '' Tpl = Template (tpl_xml) '(' output.xml ',' w ') as in c: f: ctx = context (f, data = xrange (10000000)) tpl.render_context (ctx)

The last example went to my laptop for about 20 seconds, created a (very simple) 151 MB XML file, there was no memory problem (according to the Windows Task Manager It remained constant at about 10 mm)

Depending on your needs, using this SX etc. is a friendly and quick to generate XML The method can be ... See docs to see if you can do with these engines (like others, I chose these two as examples)


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -