In [2]:
import pandas as pd, numpy as np, matplotlib.pyplot as plt, requests, urllib2
from bs4 import BeautifulSoup
%matplotlib inline

In [82]:
url='https://www.airportia.com/airports/'
s = requests.Session()
cookiesopen = s.get(url)
cookies=str(s.cookies)
fcookies=[[k[:k.find('=')],k[k.find('=')+1:k.find(' for ')]] for k in cookies[cookies.find('Cookie '):].split('Cookie ')[1:]]
#push token
opener = urllib2.build_opener()
for k in fcookies:
    opener.addheaders.append(('Cookie', k[0]+'='+k[1]))
#read html
content=s.get(url).content
soup = BeautifulSoup(content, "lxml")

In [83]:
A=[]
if len(soup.findAll(attrs={'class':'textlist'}))>0:
    links=soup.findAll(attrs={'class':'textlist'})[1].findAll('a')
A={str(i)[str(i).find('title')+7:str(i).find('>')-1]:str(i)[str(i).find('href')+6:str(i).find('title')-2]  for i in links}

In [84]:
A


Out[84]:
{'Afghanistan': '/afghanistan/',
 'Albania': '/albania/',
 'Algeria': '/algeria/',
 'American Samoa': '/american-samoa/',
 'Andorra': '/andorra/',
 'Angola': '/angola/',
 'Anguilla': '/anguilla/',
 'Antarctica': '/antarctica/',
 'Antigua And Barbuda': '/antigua-and-barbuda/',
 'Argentina': '/argentina/',
 'Armenia': '/armenia/',
 'Aruba': '/aruba/',
 'Australia': '/australia/',
 'Austria': '/austria/',
 'Azerbaijan': '/azerbaijan/',
 'Bahamas': '/bahamas/',
 'Bahrain': '/bahrain/',
 'Bangladesh': '/bangladesh/',
 'Barbados': '/barbados/',
 'Belarus': '/belarus/',
 'Belgium': '/belgium/',
 'Belize': '/belize/',
 'Benin': '/benin/',
 'Bermuda': '/bermuda/',
 'Bhutan': '/bhutan/',
 'Bolivia': '/bolivia/',
 'Bosnia And Herzegovina': '/bosnia-and-herzegovina/',
 'Botswana': '/botswana/',
 'Brazil': '/brazil/',
 'British Virgin Islands': '/british-virgin-islands/',
 'Brunei': '/brunei/',
 'Bulgaria': '/bulgaria/',
 'Burkina Faso': '/burkina-faso/',
 'Burma': '/burma/',
 'Burundi': '/burundi/',
 'Cambodia': '/cambodia/',
 'Cameroon': '/cameroon/',
 'Canada': '/canada/',
 'Cape Verde': '/cape-verde/',
 'Caribbean Netherlands': '/caribbean-netherlands/',
 'Cayman Islands': '/cayman-islands/',
 'Central African Republic': '/central-african-republic/',
 'Chad': '/chad/',
 'Chile': '/chile/',
 'China': '/china/',
 'Cocos (Keeling) Islands': '/cocos/',
 'Colombia': '/colombia/',
 'Comoros': '/comoros/',
 'Congo': '/congo/',
 'Cook Islands': '/cook-islands/',
 'Costa Rica': '/costa-rica/',
 "Cote D'ivoire (Ivory Coast)": '/ivory-coast/',
 'Croatia': '/croatia/',
 'Cuba': '/cuba/',
 'Curacao': '/cura%C3%A7ao/',
 'Cyprus': '/cyprus/',
 'Czech Republic': '/czech-republic/',
 'Democratic Republic Of The Congo': '/congo/',
 'Denmark': '/denmark/',
 'Djibouti': '/djibouti/',
 'Dominica': '/dominica/',
 'Dominican Republic': '/dominican-republic/',
 'Ecuador': '/ecuador/',
 'Egypt': '/egypt/',
 'El Salvador': '/el-salvador/',
 'Equatorial Guinea': '/equatorial-guinea/',
 'Eritrea': '/eritrea/',
 'Estonia': '/estonia/',
 'Ethiopia': '/ethiopia/',
 'Falkland Islands (Malvinas)': '/falkland-islands/',
 'Faroe Islands': '/faroe-islands/',
 'Fiji': '/fiji/',
 'Finland': '/finland/',
 'France': '/france/',
 'French Guiana': '/french-guiana/',
 'French Polynesia': '/french-polynesia/',
 'Gabon': '/gabon/',
 'Gambia': '/gambia/',
 'Georgia': '/georgia/',
 'Germany': '/germany/',
 'Ghana': '/ghana/',
 'Gibraltar': '/gibraltar/',
 'Greece': '/greece/',
 'Greenland': '/greenland/',
 'Grenada': '/grenada/',
 'Guadeloupe': '/guadeloupe/',
 'Guam': '/guam/',
 'Guatemala': '/guatemala/',
 'Guinea': '/guinea/',
 'Guinea-Bissau': '/guinea_bissau/',
 'Guyana': '/guyana/',
 'Haiti': '/haiti/',
 'Honduras': '/honduras/',
 'Hong Kong': '/hong-kong/',
 'Hungary': '/hungary/',
 'Iceland': '/iceland/',
 'India': '/india/',
 'Indonesia': '/indonesia/',
 'Iran': '/iran/',
 'Iraq': '/iraq/',
 'Ireland': '/ireland/',
 'Israel': '/israel/',
 'Italy': '/italy/',
 'Ivory Coast': '/ivory-coast/',
 'Jamaica': '/jamaica/',
 'Japan': '/japan/',
 'Jordan': '/jordan/',
 'Kazakhstan': '/kazakhstan/',
 'Kenya': '/kenya/',
 'Kiribati': '/kiribati/',
 'Kosovo': '/kosovo/',
 'Kuwait': '/kuwait/',
 'Kyrgyzstan': '/kyrgyzstan/',
 'Laos': '/laos/',
 'Latvia': '/latvia/',
 'Lebanon': '/lebanon/',
 'Lesotho': '/lesotho/',
 'Liberia': '/liberia/',
 'Libya': '/libya/',
 'Lithuania': '/lithuania/',
 'Luxembourg': '/luxembourg/',
 'Macedonia': '/macedonia/',
 'Madagascar': '/madagascar/',
 'Malawi': '/malawi/',
 'Malaysia': '/malaysia/',
 'Maldives': '/maldives/',
 'Mali': '/mali/',
 'Malta': '/malta/',
 'Marshall Islands': '/marshall-islands/',
 'Martinique': '/martinique/',
 'Mauritania': '/mauritania/',
 'Mauritius': '/mauritius/',
 'Mayotte': '/mayotte/',
 'Mexico': '/mexico/',
 'Micronesia': '/micronesia/',
 'Moldova': '/moldova/',
 'Monaco': '/monaco/',
 'Mongolia': '/mongolia/',
 'Montenegro': '/montenegro/',
 'Montserrat': '/montserrat/',
 'Morocco': '/morocco/',
 'Mozambique': '/mozambique/',
 'Myanmar (Burma)': '/burma/',
 'Namibia': '/namibia/',
 'Nauru': '/nauru/',
 'Nepal': '/nepal/',
 'Netherlands': '/netherlands/',
 'New Caledonia': '/new-caledonia/',
 'New Zealand': '/new-zealand/',
 'Nicaragua': '/nicaragua/',
 'Niger': '/niger/',
 'Nigeria': '/nigeria/',
 'North Korea': '/north-korea/',
 'Northern Mariana Islands': '/northern-mariana-islands/',
 'Norway': '/norway/',
 'Oman': '/oman/',
 'Pakistan': '/pakistan/',
 'Palau': '/palau/',
 'Palestinian Territory': '/palestinian-territory/',
 'Panama': '/panama/',
 'Papua New Guinea': '/papua-new-guinea/',
 'Paraguay': '/paraguay/',
 'Per\xc3\xba': '/per%C3%BA/',
 'Philippines': '/philippines/',
 'Poland': '/poland/',
 'Portugal': '/portugal/',
 'Puerto Rico': '/puerto-rico/',
 'Qatar': '/qatar/',
 'Reunion': '/r%C3%A9union/',
 'Romania': '/romania/',
 'Russia': '/russia/',
 'Rwanda': '/rwanda/',
 'Saint Helena': '/saint-helena/',
 'Saint Kitts And Nevis': '/saint-kitts-and-nevis/',
 'Saint Lucia': '/saint-lucia/',
 'Saint Pierre and Miquelon': '/saint-pierre-and-miquelon/',
 'Saint Vincent and the Grenadines': '/saint-vincent-and-the-grenadines/',
 'Samoa': '/samoa/',
 'Saudi Arabia': '/saudi-arabia/',
 'Senegal': '/senegal/',
 'Serbia': '/serbia/',
 'Seychelles': '/seychelles/',
 'Sierra Leone': '/sierra-leone/',
 'Singapore': '/singapore/',
 'Slovakia': '/slovakia/',
 'Slovenia': '/slovenia/',
 'Solomon Islands': '/solomon-islands/',
 'Somalia': '/somalia/',
 'South Africa': '/south-africa/',
 'South Korea': '/south-korea/',
 'South Sudan': '/south-sudan/',
 'Spain': '/spain/',
 'Sri Lanka': '/sri-lanka/',
 'Sudan': '/sudan/',
 'Suriname': '/suriname/',
 'Swaziland': '/swaziland/',
 'Sweden': '/sweden/',
 'Switzerland': '/switzerland/',
 'Syria': '/syria/',
 'S\xc3\xa3o Tom\xc3\xa9 and Principe': '/s%C3%A3o-tom%C3%A9-and-principe/',
 'Taiwan': '/taiwan/',
 'Tajikistan': '/tajikistan/',
 'Tanzania': '/tanzania/',
 'Thailand': '/thailand/',
 'Timor-Leste': '/timor_leste/',
 'Timor-Leste (East Timor)': '/timor_leste/',
 'Togo': '/togo/',
 'Tonga': '/tonga/',
 'Trinidad And Tobago': '/trinidad-and-tobago/',
 'Tunisia': '/tunisia/',
 'Turkey': '/turkey/',
 'Turkmenistan': '/turkmenistan/',
 'Turks and Caicos Islands': '/turks-and-caicos-islands/',
 'Tuvalu': '/tuvalu/',
 'U.S. Virgin Islands': '/u.s.-virgin-islands/',
 'Uganda': '/uganda/',
 'Ukraine': '/ukraine/',
 'United Arab Emirates': '/united-arab-emirates/',
 'United Kingdom': '/united-kingdom/',
 'United States': '/united-states/',
 'United States Minor Outlying Islands': '/united-states-minor-outlying-islands/',
 'Uruguay': '/uruguay/',
 'Uzbekistan': '/uzbekistan/',
 'Vanuatu': '/vanuatu/',
 'Venezuela': '/venezuela/',
 'Vietnam': '/vietnam/',
 'Virgin Islands, British': '/british-virgin-islands/',
 'Virgin Islands, Us': '/u.s.-virgin-islands/',
 'Wallis And Futuna': '/wallis-and-futuna/',
 'Western Sahara': '/western-sahara/',
 'Yemen': '/yemen/',
 'Zambia': '/zambia/',
 'Zimbabwe': '/zimbabwe/'}

Remove duplicates


In [85]:
D={}
for i in A:
    if A[i] not in D:D[A[i]]=[]
    D[A[i]].append(i)
for i in D:
    if len(D[i])>1:
        A.pop(D[i][0]);

Capture airport lists


In [86]:
L={}
M={}
N={}
F=[]
baseurl='https://www.airportia.com'

In [87]:
for c in A:
    if c not in L or F:
        #capture token
        url=baseurl+A[c]
        s = requests.Session()
        cookiesopen = s.get(url)
        cookies=str(s.cookies)
        fcookies=[[k[:k.find('=')],k[k.find('=')+1:k.find(' for ')]] for k in cookies[cookies.find('Cookie '):].split('Cookie ')[1:]]
        #push token
        opener = urllib2.build_opener()
        for k in fcookies:
            opener.addheaders.append(('Cookie', k[0]+'='+k[1]))
        #read html
        content=s.get(url).content
        soup = BeautifulSoup(content, "lxml")
        if len(soup.findAll(attrs={'class':'textlist'}))>0:
            links=soup.findAll(attrs={'class':'textlist'})[0].findAll('a')
            L[c]=[str(i)[str(i).find('href')+6:str(i).find('title')-2] for i in links]
            M[c]=[str(i)[str(i).find('title')+7:str(i).find('>')-1] for i in links]  
            N[c]=[str(i)[str(i).find('<')-8:str(i).find('<')-5] for i in links]
            print 'Success',url
        else:
            F.append(c)
            print 'Fail',url


Success https://www.airportia.com/canada/
Success https://www.airportia.com/turkmenistan/
Success https://www.airportia.com/saint-helena/
Success https://www.airportia.com/lithuania/
Success https://www.airportia.com/cambodia/
Success https://www.airportia.com/switzerland/
Success https://www.airportia.com/ethiopia/
Success https://www.airportia.com/aruba/
Success https://www.airportia.com/swaziland/
Success https://www.airportia.com/argentina/
Success https://www.airportia.com/bolivia/
Success https://www.airportia.com/cameroon/
Success https://www.airportia.com/burkina-faso/
Success https://www.airportia.com/ghana/
Success https://www.airportia.com/saudi-arabia/
Success https://www.airportia.com/japan/
Success https://www.airportia.com/cape-verde/
Success https://www.airportia.com/united-states-minor-outlying-islands/
Success https://www.airportia.com/cocos/
Success https://www.airportia.com/slovenia/
Success https://www.airportia.com/guatemala/
Success https://www.airportia.com/kuwait/
Success https://www.airportia.com/jordan/
Success https://www.airportia.com/dominica/
Success https://www.airportia.com/liberia/
Success https://www.airportia.com/maldives/
Success https://www.airportia.com/jamaica/
Success https://www.airportia.com/oman/
Success https://www.airportia.com/tanzania/
Success https://www.airportia.com/martinique/
Success https://www.airportia.com/saint-kitts-and-nevis/
Success https://www.airportia.com/zambia/
Success https://www.airportia.com/albania/
Success https://www.airportia.com/gabon/
Success https://www.airportia.com/saint-pierre-and-miquelon/
Success https://www.airportia.com/monaco/
Success https://www.airportia.com/new-zealand/
Success https://www.airportia.com/yemen/
Success https://www.airportia.com/andorra/
Success https://www.airportia.com/greenland/
Success https://www.airportia.com/samoa/
Success https://www.airportia.com/united-arab-emirates/
Success https://www.airportia.com/guam/
Success https://www.airportia.com/uruguay/
Success https://www.airportia.com/india/
Success https://www.airportia.com/azerbaijan/
Success https://www.airportia.com/lesotho/
Success https://www.airportia.com/saint-vincent-and-the-grenadines/
Success https://www.airportia.com/kenya/
Success https://www.airportia.com/south-korea/
Success https://www.airportia.com/tajikistan/
Success https://www.airportia.com/northern-mariana-islands/
Success https://www.airportia.com/turkey/
Success https://www.airportia.com/afghanistan/
Success https://www.airportia.com/bangladesh/
Success https://www.airportia.com/mauritania/
Success https://www.airportia.com/solomon-islands/
Success https://www.airportia.com/turks-and-caicos-islands/
Success https://www.airportia.com/saint-lucia/
Success https://www.airportia.com/hungary/
Success https://www.airportia.com/french-polynesia/
Success https://www.airportia.com/france/
Success https://www.airportia.com/western-sahara/
Success https://www.airportia.com/vietnam/
Success https://www.airportia.com/bermuda/
Success https://www.airportia.com/slovakia/
Success https://www.airportia.com/somalia/
Success https://www.airportia.com/laos/
Success https://www.airportia.com/nauru/
Success https://www.airportia.com/seychelles/
Success https://www.airportia.com/norway/
Success https://www.airportia.com/malawi/
Success https://www.airportia.com/cook-islands/
Success https://www.airportia.com/benin/
Success https://www.airportia.com/wallis-and-futuna/
Success https://www.airportia.com/cuba/
Success https://www.airportia.com/montenegro/
Success https://www.airportia.com/falkland-islands/
Success https://www.airportia.com/togo/
Success https://www.airportia.com/china/
Success https://www.airportia.com/armenia/
Success https://www.airportia.com/dominican-republic/
Success https://www.airportia.com/mongolia/
Success https://www.airportia.com/ukraine/
Success https://www.airportia.com/bahrain/
Success https://www.airportia.com/tonga/
Success https://www.airportia.com/finland/
Success https://www.airportia.com/libya/
Success https://www.airportia.com/cayman-islands/
Success https://www.airportia.com/central-african-republic/
Success https://www.airportia.com/mauritius/
Success https://www.airportia.com/sweden/
Success https://www.airportia.com/australia/
Success https://www.airportia.com/antigua-and-barbuda/
Success https://www.airportia.com/mali/
Success https://www.airportia.com/russia/
Success https://www.airportia.com/bulgaria/
Success https://www.airportia.com/united-states/
Success https://www.airportia.com/romania/
Success https://www.airportia.com/angola/
Success https://www.airportia.com/chad/
Success https://www.airportia.com/south-africa/
Success https://www.airportia.com/cyprus/
Success https://www.airportia.com/caribbean-netherlands/
Success https://www.airportia.com/qatar/
Success https://www.airportia.com/malaysia/
Success https://www.airportia.com/austria/
Success https://www.airportia.com/per%C3%BA/
Success https://www.airportia.com/mozambique/
Success https://www.airportia.com/uganda/
Success https://www.airportia.com/british-virgin-islands/
Success https://www.airportia.com/niger/
Success https://www.airportia.com/brazil/
Success https://www.airportia.com/faroe-islands/
Success https://www.airportia.com/guinea/
Success https://www.airportia.com/panama/
Success https://www.airportia.com/costa-rica/
Success https://www.airportia.com/luxembourg/
Success https://www.airportia.com/american-samoa/
Success https://www.airportia.com/bahamas/
Success https://www.airportia.com/gibraltar/
Success https://www.airportia.com/ireland/
Success https://www.airportia.com/pakistan/
Success https://www.airportia.com/palau/
Success https://www.airportia.com/nigeria/
Success https://www.airportia.com/ecuador/
Success https://www.airportia.com/czech-republic/
Success https://www.airportia.com/brunei/
Success https://www.airportia.com/belarus/
Success https://www.airportia.com/iran/
Success https://www.airportia.com/algeria/
Success https://www.airportia.com/el-salvador/
Success https://www.airportia.com/tuvalu/
Success https://www.airportia.com/marshall-islands/
Success https://www.airportia.com/chile/
Success https://www.airportia.com/puerto-rico/
Success https://www.airportia.com/belgium/
Success https://www.airportia.com/kiribati/
Success https://www.airportia.com/haiti/
Success https://www.airportia.com/belize/
Success https://www.airportia.com/hong-kong/
Success https://www.airportia.com/sierra-leone/
Success https://www.airportia.com/georgia/
Success https://www.airportia.com/gambia/
Success https://www.airportia.com/philippines/
Success https://www.airportia.com/moldova/
Success https://www.airportia.com/morocco/
Success https://www.airportia.com/croatia/
Success https://www.airportia.com/micronesia/
Success https://www.airportia.com/guinea_bissau/
Success https://www.airportia.com/thailand/
Success https://www.airportia.com/namibia/
Success https://www.airportia.com/grenada/
Success https://www.airportia.com/ivory-coast/
Success https://www.airportia.com/u.s.-virgin-islands/
Success https://www.airportia.com/iraq/
Success https://www.airportia.com/portugal/
Success https://www.airportia.com/estonia/
Success https://www.airportia.com/kosovo/
Success https://www.airportia.com/mexico/
Success https://www.airportia.com/lebanon/
Success https://www.airportia.com/uzbekistan/
Success https://www.airportia.com/tunisia/
Success https://www.airportia.com/djibouti/
Success https://www.airportia.com/rwanda/
Success https://www.airportia.com/timor_leste/
Success https://www.airportia.com/spain/
Success https://www.airportia.com/colombia/
Success https://www.airportia.com/r%C3%A9union/
Success https://www.airportia.com/burundi/
Success https://www.airportia.com/taiwan/
Success https://www.airportia.com/fiji/
Success https://www.airportia.com/barbados/
Success https://www.airportia.com/madagascar/
Success https://www.airportia.com/italy/
Success https://www.airportia.com/cura%C3%A7ao/
Success https://www.airportia.com/bhutan/
Success https://www.airportia.com/sudan/
Success https://www.airportia.com/nepal/
Success https://www.airportia.com/malta/
Success https://www.airportia.com/netherlands/
Success https://www.airportia.com/bosnia-and-herzegovina/
Success https://www.airportia.com/suriname/
Success https://www.airportia.com/anguilla/
Success https://www.airportia.com/s%C3%A3o-tom%C3%A9-and-principe/
Success https://www.airportia.com/israel/
Success https://www.airportia.com/indonesia/
Success https://www.airportia.com/iceland/
Success https://www.airportia.com/venezuela/
Success https://www.airportia.com/senegal/
Success https://www.airportia.com/papua-new-guinea/
Success https://www.airportia.com/zimbabwe/
Success https://www.airportia.com/germany/
Success https://www.airportia.com/vanuatu/
Success https://www.airportia.com/denmark/
Success https://www.airportia.com/kazakhstan/
Success https://www.airportia.com/poland/
Success https://www.airportia.com/eritrea/
Success https://www.airportia.com/kyrgyzstan/
Success https://www.airportia.com/palestinian-territory/
Success https://www.airportia.com/mayotte/
Success https://www.airportia.com/montserrat/
Success https://www.airportia.com/new-caledonia/
Success https://www.airportia.com/macedonia/
Success https://www.airportia.com/north-korea/
Success https://www.airportia.com/sri-lanka/
Success https://www.airportia.com/latvia/
Success https://www.airportia.com/south-sudan/
Success https://www.airportia.com/guyana/
Success https://www.airportia.com/syria/
Success https://www.airportia.com/guadeloupe/
Success https://www.airportia.com/burma/
Success https://www.airportia.com/honduras/
Success https://www.airportia.com/equatorial-guinea/
Success https://www.airportia.com/egypt/
Success https://www.airportia.com/nicaragua/
Success https://www.airportia.com/singapore/
Success https://www.airportia.com/serbia/
Success https://www.airportia.com/botswana/
Success https://www.airportia.com/united-kingdom/
Success https://www.airportia.com/trinidad-and-tobago/
Success https://www.airportia.com/antarctica/
Success https://www.airportia.com/congo/
Success https://www.airportia.com/greece/
Success https://www.airportia.com/paraguay/
Success https://www.airportia.com/french-guiana/
Success https://www.airportia.com/comoros/

In [88]:
F


Out[88]:
[]

Save links


In [89]:
import json
file('../json/L.json','w').write(json.dumps(L))
file('../json/M.json','w').write(json.dumps(M))
file('../json/N.json','w').write(json.dumps(N))

Create folder structure


In [90]:
iso2=pd.read_html('https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2')

In [91]:
I2={}
for i in iso2[2].T.set_index(0).iteritems():
    country=i[1][1]
    code=i[1][0]
    if str(code)=='nan':code=u'NA'
    I2[country]=code

In [92]:
cnc_path='../../universal/countries/'
cnc=pd.read_excel(cnc_path+'cnc.xlsx').set_index('Name')

In [93]:
I3={}
err=[]
for i in L:
    if i in I2:
        I3[i]=I2[i]
    elif i in cnc.columns:
        I3[i]=cnc.loc['ISO2'][i]
    else: err.append(i)

In [94]:
err


Out[94]:
['Saint Helena',
 'Wallis And Futuna',
 'Palestinian Territory',
 'Saint Kitts And Nevis',
 'South Korea',
 'Laos',
 'Antigua And Barbuda',
 'Russia',
 'Caribbean Netherlands',
 'Syria',
 'Brunei',
 'Moldova',
 "Cote D'ivoire (Ivory Coast)",
 'U.S. Virgin Islands',
 'Reunion',
 'Curacao',
 'Micronesia',
 'Bosnia And Herzegovina',
 'S\xc3\xa3o Tom\xc3\xa9 and Principe',
 'Macedonia',
 'North Korea',
 'Per\xc3\xba',
 'Burma',
 'Trinidad And Tobago']

Manual fix conflicts


In [95]:
E2={
 'Saint Helena':'SH',
 'Wallis And Futuna':'WF',
 'Palestinian Territory':'PS',
 'Saint Kitts And Nevis':'KN',
 'South Korea':'KR',
 'Laos':'LA',
 'Antigua And Barbuda':'AG',
 'Russia':'RU',
 'Caribbean Netherlands':'BQ',
 'Syria':'SY',
 'Brunei':'BN',
 'Moldova':'MD',
 "Cote D'ivoire (Ivory Coast)":'CI',
 'U.S. Virgin Islands':'VI',
 'Reunion':'RE',
 'Curacao':'CW',
 'Micronesia':'FM',
 'Bosnia And Herzegovina':'BA',
 'S\xc3\xa3o Tom\xc3\xa9 and Principe':'ST',
 'Macedonia':'MK',
 'North Korea':'KP',
 'Per\xc3\xba':'PE',
 'Burma':'MM',
 'Trinidad And Tobago':'TT'
}

In [100]:
for i in E2:
    I3[i]=E2[i]

In [118]:
I3['Kosovo']='XK'

In [104]:
len(I3)==len(A)


Out[104]:
True

In [119]:
file('../json/I3.json','w').write(json.dumps(I3))

Save


In [109]:
pd.DataFrame(I3,index=['ISO2']).to_csv(cnc_path+'cnc.csv')
#then manually edit, create legible

In [112]:
cnc=pd.read_csv(cnc_path+'cnc.csv').set_index('index')
cnc


Out[112]:
Afghanistan Albania Algeria American Samoa Andorra Angola Anguilla Antarctica Antigua And Barbuda Argentina ... Uruguay Uzbekistan Vanuatu Venezuela Vietnam Wallis And Futuna Western Sahara Yemen Zambia Zimbabwe
index
ISO2 AF AL DZ AS AD AO AI AQ AG AR ... UY UZ VU VE VN WF EH YE ZM ZW
pretty Afghanistan Albania Algeria American Samoa Andorra Angola Anguilla Antarctica Antigua & Barbuda Argentina ... Uruguay Uzbekistan Vanuatu Venezuela Vietnam Wallis & Futuna Western Sahara Yemen Zambia Zimbabwe

2 rows × 227 columns

Create folder structure


In [24]:
import os.path

In [120]:
for i in I3:
    directory='../countries/'+I3[i].lower()
    if not os.path.exists(directory) :
        os.makedirs(directory)
    for j in ['code','d3','json','map']:
        if not os.path.exists(directory+'/'+j):
            os.makedirs(directory+'/'+j)