Skip to content

Instantly share code, notes, and snippets.

@pjcunningham
Last active December 24, 2020 18:07
Show Gist options
  • Save pjcunningham/e25b38a217510d01c7a77403ba57dccf to your computer and use it in GitHub Desktop.
Save pjcunningham/e25b38a217510d01c7a77403ba57dccf to your computer and use it in GitHub Desktop.
Extract a specify country's locations from openweathermap city list. Very fast.
# coding: utf-8
__author__ = 'Paul Cunningham'
__copyright = 'Copyright 2020, Paul Cunningham'
from jsonstreams import Stream
from jsonstreams import Type
import json
import ijson
from decimal import Decimal
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return float(obj)
return json.JSONEncoder.default(self, obj)
# http://bulk.openweathermap.org/sample/city.list.json.gz
source_file_path = 'C:/Users/Paul/Downloads/city.list.json'
sink_file_path = 'gb.city.list.json'
with Stream(Type.array, filename=sink_file_path, encoder=DecimalEncoder, indent=4, pretty=True) as sink_stream:
with open(source_file_path, 'rb') as source_file:
items = ijson.items(source_file, 'item')
filtered_cities = (o for o in items if o['country'] == 'GB')
for city in filtered_cities:
sink_stream.write(city)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment