Last active
August 29, 2015 14:06
-
-
Save yackermann/11e9c3be03e326975da6 to your computer and use it in GitHub Desktop.
SHITCODED COUNTRIES.JSON STYLER! PLEASE KILL THE AUTHOR!!!
This file contains 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 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