Skip to content

Instantly share code, notes, and snippets.

@Zia-
Last active April 18, 2016 13:52
Show Gist options
  • Save Zia-/3252a5118b9e8c92990cfd4336a0e97d to your computer and use it in GitHub Desktop.
Save Zia-/3252a5118b9e8c92990cfd4336a0e97d to your computer and use it in GitHub Desktop.
from lxml import etree as et
from bz2file import BZ2File
from lxml.etree import tostring
path = "<path-to-osm.bz2-dir>/planet.osm.bz2"
path_nodes = "<path-to-osm.bz2-dir>/planet_nodes.osm"
str = '';
with BZ2File(path) as xml_file:
with open(path_nodes, 'w') as f:
f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
f.write("<osm>")
parser = et.iterparse(xml_file, events=('end',))
for events, elem in parser:
if elem.tag == "node":
str = str + tostring(elem);
f.write(str)
f.write("</osm>")
@Zia-
Copy link
Author

Zia- commented Apr 18, 2016

Write event is taking place, now, only once for all the looped elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment