tpl_xml = """
<doc xmlns:py="http://genshi.edgewall.org/">
<p py:for="i in data">${i}</p>
</doc>
"""
tpl = MarkupTemplate(tpl_xml)
stream = tpl.generate(data=range(10000000))
with open('output.xml', 'w') as f:
    stream.render(out=f)
Generating very large XML files in Python?