Skip to content

Instantly share code, notes, and snippets.

@Sukonnik-Illia
Created October 5, 2015 15:18
Show Gist options
  • Save Sukonnik-Illia/7419f0a1f50530ba30e5 to your computer and use it in GitHub Desktop.
Save Sukonnik-Illia/7419f0a1f50530ba30e5 to your computer and use it in GitHub Desktop.
low memory lxml.etree.iterparse
from lxml import etree
def fast_iter(context, func, *args, **kwargs):
""" call func on each xml element chosen when create context
context = etree.iterparse('path to file', tag='your tag', events)
"""
for event, elem in context:
func(elem, *args, *kwargs)
elem.clear()
while elem.getprevious() is not None:
del elem.getparent()[0]
del context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment