import pycountry
user_input = raw_input(': ')
mapping = {country.name: country.alpha_2 for country in pycountry.countries}
print mapping.get(user_input)
is the correct way you are using 'alpha2' instead of alpha_2
Answer from Afsal Salim on Stack Overflow
» pip install pycountry-convert
» pip install pycountry
import pycountry
user_input = raw_input(': ')
mapping = {country.name: country.alpha_2 for country in pycountry.countries}
print mapping.get(user_input)
is the correct way you are using 'alpha2' instead of alpha_2
For Python3, I believe that using the Pytz built-in module will be easier
>> import pytz
>> print(pytz.country_names['tn'])
Tunisia
You can achieve this with the following:
alpha2 = pycountry.subdivisions.lookup("Beroun").country_code # "CZ"
# If you're looking for alpha_3:
pycountry.countries.get(alpha_2=alpha2).alpha_3 # "CZE"
However, you have to be careful of ambiguous locations e.g. the US State "Montana"
pycountry.subdivisions.lookup("Montana").country_code # BG
You could get the complete address i.e. long/lat/city/countryname using geopy:
from geopy import geocoders
Google-API-KEY = "xxx-xxxx-xxxxxxxxx-xxxxx"
gn = geocoders.GoogleV3(Google-API-KEY)
place, (lat, lng) = gn.geocode("Beroun")
print(place)
OUTPUT:
266 01 Beroun, Czechia
Once you have the country name, you can then get its abbreviation using pycountry:
import pycountry
user_input = 'Czechia'
mapping = {country.name: country.alpha_2 for country in pycountry.countries}
print(mapping.get(user_input))
OUTPUT:
CZ
Now you know, for a state, it's country and the abbreviation of it. Create a dictionary out of states and its countries, if you want the key-value paired of state-country
perhaps something like:
state_country = {'Beroun': 'CZ'}
print(state_country)
OUTPUT:
{'Beroun': 'CZ'}
Use pycountry.countries.lookup
>>> pycountry.countries.lookup('Bolivia')
Country(alpha_2='BO', alpha_3='BOL', common_name='Bolivia', name='Bolivia, Plurinational State of', numeric='068', official_name='Plurinational State of Bolivia')
This, albeit not being elegant at all, worked for me:
def country_code_converter(input_countries):
"""
:param input_countries: list containing the name of the countries (can be numpy array)
:return: list with the ISO alpha 3 codes for the given input ('Unknown Country' if no match found)
"""
countries = {}
countries_official = {}
countries_common = {}
#loops over all of the countries contained in the pycountry library and populates dictionary
for country in pycountry.countries:
countries[country.name] = country.alpha_3
#loops over the alpha_3 codes from the countries dictionary
#populates dictionary containing official names and codes
for alpha_3 in list(countries.values()):
try:
countries_official[pycountry.countries.get(alpha_3 = alpha_3).official_name] = alpha_3
except:
None
#same for common names
for alpha_3 in list(countries.values()):
try:
countries_common[pycountry.countries.get(alpha_3 = alpha_3).common_name] = alpha_3
except:
None
codes = []
# appends ISO codes for all matches by trying different country name types
# appends Unknown Country if no match found
for i in input_countries:
if i in countries.keys():
codes.append(countries.get(i))
elif i in countries_official.keys():
codes.append(countries_official.get(i))
elif i in countries_common.keys():
codes.append(countries_common.get(i))
else:
codes.append('Unknown Country')
return codes