pycountry-convert: Country and Continent Code Conversion
pycountry-convert is a Python library that extends `pycountry` by providing conversion functions between ISO country names, various country codes (Alpha-2, Alpha-3), and continent names. It facilitates mapping between these geographical identifiers. The current version is 0.7.2, released in 2018, indicating an infrequent release cadence.
Warnings
- gotcha Direct conversion from country name to continent name is not supported. Users must first convert the country name to an ISO Alpha-2 code, then convert the Alpha-2 code to a continent code, and finally convert the continent code to its name.
- gotcha The `pycountry-convert` library has not been updated since version 0.7.2 in February 2018. This means its internal data for countries and continents may be outdated and might not reflect recent geopolitical changes or updates to ISO standards, even if its dependency `pycountry` (which has seen more recent updates) is installed separately.
- gotcha For large datasets, repeatedly calling conversion functions can be slow due to re-computation. The library itself does not inherently cache results between calls.
Install
-
pip install pycountry-convert
Imports
- pycountry_convert
import pycountry_convert as pc
Quickstart
import pycountry_convert as pc
# Convert country name to Alpha-2 code
country_name = 'Germany'
try:
country_alpha2 = pc.country_name_to_country_alpha2(country_name, cn_name_format='default')
print(f"Alpha-2 code for {country_name}: {country_alpha2}")
# Convert Alpha-2 code to continent code
continent_code = pc.country_alpha2_to_continent_code(country_alpha2)
print(f"Continent code for {country_alpha2}: {continent_code}")
# Convert continent code to continent name
continent_name = pc.convert_continent_code_to_continent_name(continent_code)
print(f"Continent name for {continent_code}: {continent_name}")
except KeyError:
print(f"Could not find data for country: {country_name}")