-
-
Save akr4/3f367354a0026a1925ca91717a7a593b to your computer and use it in GitHub Desktop.
Python's XMLPullParser runs out of memory
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
import xml.etree.ElementTree as ET | |
import argparse | |
arg_parser = argparse.ArgumentParser() | |
arg_parser.add_argument('in_file') | |
args = arg_parser.parse_args() | |
def main(args): | |
with open(args.in_file) as f: | |
parser = ET.XMLPullParser(['start', 'end']) | |
for line in f: | |
parser.feed(line) | |
for event, elem in parser.read_events(): | |
pass | |
main(args) |
Using lxml or cElementTree and calling element.clear() solved the problem.
import lxml.etree as ET
import argparse
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('in_file')
args = arg_parser.parse_args()
def main(args):
with open(args.in_file, 'rb') as f:
it = ET.iterparse(f, ['start', 'end'])
for event, elem in it:
elem.clear()
main(args)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This seems to be intended behavior.
http://docs.python.jp/3/library/xml.etree.elementtree.html