Make script for json out files



In [1]:
import pandas as pd
import json

Get csv from countrycode repo


In [2]:
df = pd.read_csv('https://raw.githubusercontent.com/vincentarelbundock/countrycode/master/data/countrycode_data.csv')

df.head()


Out[2]:
country_name cowc cown fao fips104 imf ioc iso2c iso3c iso3n un wb regex continent region
0 Afghanistan AFG 700 2 AF 512 AFG AF AFG 4 4 AFG afghan Asia Southern Asia
1 Aland Islands NaN NaN NaN NaN NaN NaN AX ALA 248 248 ALA \b(a|å)land Europe Northern Europe
2 Albania ALB 339 3 AL 914 ALB AL ALB 8 8 ALB albania Europe Southern Europe
3 Algeria ALG 615 4 AG 612 ALG DZ DZA 12 12 DZA algeria Africa Northern Africa
4 American Samoa NaN NaN NaN AQ 859 ASA AS ASM 16 16 ASM ^(?=.*americ).*samoa Oceania Polynesia

5 rows × 15 columns


In [3]:
df = df.dropna(subset=['iso3c'])

Output array of regex strings, iso3 objects


In [4]:
outfile = 'get-iso3.json'
out = []

for iso3, regex in zip(df['iso3c'], df['regex']):
    out.append(dict(iso3=iso3, regex=regex))
    
with open(outfile, 'w') as f:
    json.dump(out, f, separators=(',',':'))

Output iso3 to regex strings hash object


In [5]:
outfile = 'country-name_to_iso3.json'
out = dict()

for iso3, regex in zip(df['iso3c'], df['regex']):
    out[iso3] = regex
    
with open(outfile, 'w') as f:
    json.dump(out, f, separators=(',',':'))

Output iso3 to country name hash object


In [6]:
outfile = 'iso3-to-name.json'
out = dict()

for iso3, country_name in zip(df['iso3c'], df['country_name']):
    out[iso3] = country_name
    
with open(outfile, 'w') as f:
    json.dump(out, f, separators=(',',':'))