Last active
December 24, 2020 18:07
-
-
Save pjcunningham/e25b38a217510d01c7a77403ba57dccf to your computer and use it in GitHub Desktop.
Extract a specify country's locations from openweathermap city list. Very fast.
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
# 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