Created
July 29, 2016 13:26
-
-
Save ahoereth/68e53776c1d74a9ccf8f0fe6d971a197 to your computer and use it in GitHub Desktop.
Converts country 'geoname' data from http://www.geonames.org/export/ to JSON.
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
import os | |
import sys | |
import csv | |
import json | |
filename = sys.argv[1] | |
name, _ = os.path.splitext(filename) | |
header = ['geonameid', 'name', 'asciiname', 'alternatenames', | |
'latitude', 'longitude', 'featureclass', 'featurecode', | |
'countrycode', 'cc2', 'admin1code', 'admin2code', 'admin3code', | |
'admin4code', 'population', 'elevation', 'dem', 'timezone', 'date'] | |
records = [] | |
with open(filename, mode='r', encoding='ISO-8859-1') as src: | |
reader = csv.reader(src, delimiter='\t', quotechar='"') | |
for row in reader: | |
records.append({key: row[i] for (i, key) in enumerate(header)}) | |
with open('%s.json' % name, 'w') as dst: | |
json.dump(records, dst, indent=' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment