Skip to content

Instantly share code, notes, and snippets.

@herrjemand
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herrjemand/11e9c3be03e326975da6 to your computer and use it in GitHub Desktop.
Save herrjemand/11e9c3be03e326975da6 to your computer and use it in GitHub Desktop.
SHITCODED COUNTRIES.JSON STYLER! PLEASE KILL THE AUTHOR!!!
import json
x = {}
with open('countries.json') as f:
x = json.loads(f.read())
jzon = ''
for i in x:
codes = {
"name": "\"name\":",
"tld": "\"tld\":",
"cca2": "\"cca2\":",
"ccn3": "\"ccn3\":",
"cca3": "\"cca3\":",
"currency": "\"currency\":",
"callingCode": "\"callingCode\":",
"capital": "\"capital\":",
"altSpellings": "\"altSpellings\":",
"relevance": "\"relevance\":",
"region": "\"region\":",
"subregion": "\"subregion\":",
"languages": "\"languages\":",
"translations": "\"translations\":",
"latlng": "\"latlng\":",
"demonym": "\"demonym\":",
"landlocked": "\"landlocked\":",
"borders": "\"borders\":",
"area": "\"area\":"
}
for n in i:
if type(i[n]) in [str,int,float,list,bool]:
codes[n] = codes[n] + ' ' + json.dumps(i[n])
else:
if n in ['languages']:
codes[n] = codes[n] + " {\n"
if len(i['languages'].keys()):
for z in sorted(i[n]):
codes[n] = codes[n] + "\t\t\t\"" + z + "\": " + json.dumps(i[n][z]) + ",\n"
codes[n] = codes[n][:-2] + '\n'
codes[n] = codes[n] + "\t\t}"
else:
codes[n] = codes[n][:-1:] + '}'
elif n == 'translations':
codes[n] = codes[n] + ' {\n'
if len(i['translations'].keys()):
for k in sorted(i['translations'].keys()):
codes[n] += '\t'*3 + '"' + k + '": { ' \
+ '\"official\": ' + json.dumps(i['translations'][k]['official']) + ','\
+ ' ' + '\"common\": ' + json.dumps(i['translations'][k]['common'])\
+ ' },\n'
codes[n] = codes[n][:-2:]
codes[n] += '\n\t\t}'
else:
codes[n] = codes[n][:-1:] + '}'
elif n == 'name':
codes[n] = codes[n] + ' {\n' \
+ '\t\t\t\"common\": ' + json.dumps(i[n]['common']) + ',\n' \
+ '\t\t\t\"official\": ' + json.dumps(i[n]['official']) + ',\n' \
+ '\t\t\t\"native\": {\n'
if len(i['name']['native'].keys()):
for k in sorted(i['name']['native'].keys()):
codes[n] += '\t'*4 + '"' + k + '": {\n' \
+ '\t'*5 + '\"official\": ' + json.dumps(i['name']['native'][k]['official']) + ',\n'\
+ '\t'*5 + '\"common\": ' + json.dumps(i['name']['native'][k]['common']) + '\n'\
+ '\t'*4 + '},\n'
codes[n] = codes[n][:-2:]
codes[n] += '\n\t\t\t}\n\t\t}'
else:
codes[n] = codes[n][:-1:] + '}\n\t\t}'
string = ''
order = [
"name",
"tld",
"cca2",
"ccn3",
"cca3",
"currency",
"callingCode",
"capital",
"altSpellings",
"relevance",
"region",
"subregion",
"languages",
"translations",
"latlng",
"demonym",
"landlocked",
"borders",
"area"
]
for i in order:
string = string + '\t\t' + codes[i] + ",\n"
jzon += '\n\t{\n' + string[:-2] + '\n\t},'
jzon = '[\n' + jzon[:-1] + '\n]'
with open('countries.json','w') as w:
w.write(jzon)
import json
habababa = ''
with open('countries.json') as f:
habababa = json.loads(f.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment