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')
Answer from pacholik on Stack Overflowpython - Using pycountry to check for name/common_name/official_name - Stack Overflow
How to convert country names to ISO 3166-1 alpha-2 values, using python - Stack Overflow
isocodes: A python library that provides you access to lists of various ISO standards (e.g. country, language, language scripts, and currency names).
The DRC in Pycountry
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
There is a module called pycountry.
Here's an example code:
import pycountry
input_countries = ['American Samoa', 'Canada', 'France']
countries = {}
for country in pycountry.countries:
countries[country.name] = country.alpha_2
codes = [countries.get(country, 'Unknown code') for country in input_countries]
print(codes) # prints ['AS', 'CA', 'FR']
You can use this csv file : country code list into a CSV.
import csv
dic = {}
with open("wikipedia-iso-country-codes.csv") as f:
file= csv.DictReader(f, delimiter=',')
for line in file:
dic[line['English short name lower case']] = line['Alpha-2 code']
countries = ['American Samoa', 'Canada', 'France']
for country in countries:
print(dic[country])
Will print:
AS
CA
FR
Few more alternatives.
» pip install pycountry-convert
Hi,
For a Django project, I wanted to get a list of all countries.
I went using pytz because I did not wanted to use django-countries but since now pytz is removed from Django, I had to find an alternative.
I quickly looked and found pycountries that not only provides countries but also languages and currencies which I also need in my project.
Problem, pycountries seems dead and data is outdated at this point.
So I decided to start a new library and isocodes is born.
The data is coming from https://salsa.debian.org/iso-codes-team/iso-codes, many thanks to them.
It has support for all the databases of iso-codes as well as the translations and a few methods to extract the data or sort it.
I would say that it's usable, please report any issue you would find !
Github: https://github.com/Atem18/isocodes
Pypi: https://pypi.org/project/isocodes/
Hey, I am trying to change country names to ones that are recognised by pycountry so that I can get their ISO-3 codes, does anyone know what it is for the DRC (Democratic Republic of the Congo)?
Wikipedia says: 'Congo, Democratic Republic of the' but it is not working for me
Edit: its 'Congo, The Democratic Republic of the' if anyone cares