Created
September 19, 2018 07:12
-
-
Save bashkirtsevich/3f98cfc7e52cc5bd8e4c79926246b87d to your computer and use it in GitHub Desktop.
Read xml from bz2
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 as et | |
| from bz2file import BZ2File | |
| path = "where/my/fileis.osm.bz2" | |
| with BZ2File(path) as xml_file: | |
| parser = et.iterparse(xml_file, events=('end',)) | |
| for events, elem in parser: | |
| if elem.tag == "tag": | |
| continue | |
| if elem.tag == "node": | |
| (do something) | |
| ## Do some cleaning | |
| # Get rid of that element | |
| elem.clear() | |
| # Also eliminate now-empty references from the root node to node | |
| while elem.getprevious() is not None: | |
| del elem.getparent()[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment