Skip to content

Instantly share code, notes, and snippets.

@bashkirtsevich
Created September 19, 2018 07:12
Show Gist options
  • Select an option

  • Save bashkirtsevich/3f98cfc7e52cc5bd8e4c79926246b87d to your computer and use it in GitHub Desktop.

Select an option

Save bashkirtsevich/3f98cfc7e52cc5bd8e4c79926246b87d to your computer and use it in GitHub Desktop.
Read xml from bz2
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