4 ms·
Why are you using regular expressions to parse XML? Python has excellent XML libraries.
by mercutio2 9y ago
Why are you using regular expressions to parse XML? Python has excellent XML libraries.
- minimaxir 9y agoBecause reading line-by-line is better than loading-and-parsing a 1 GB (in my case) XML file.
- icebraining 9y agoIterative parsing is your friend: http://boscoh.com/programming/reading-xml-serially.html http://boscoh.com/programming/reading-xml-serially.html There used to be a big argument between DOM and SAX parsers, but iterative/streaming parsers nowadays offer a pretty good compromise.
- mercutio2 9y agoThat link has some bizarre misconceptions about namespaces (just define the namespace URLs you’re interested in as constants, don’t depend on the particular short name/URL map you expect, as that guy does). It also looks like it predates cElementTree, which has an identical API but is vastly faster. But anyway, yes, iterparse will be much more reliable/flexible than a regular expression search of non-canonical XML data, and will likely be faster, too.
- deleted 9y ago[deleted]