Created
October 5, 2015 15:18
-
-
Save Sukonnik-Illia/7419f0a1f50530ba30e5 to your computer and use it in GitHub Desktop.
low memory lxml.etree.iterparse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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