Exploring Countries with the pycountry Module in Python

Posted by Afsal on 01-Sep-2023

Hi Pythonistas!

It is challenging to collect while working on the projects with countries and currencies data. Today we will learn a module with all the data, pycountry. Let us dive into the code.

Installation

pip install pycountry

Listing countries

import pycountry

countries = pycountry.countries

for country in countries:
    print(country.name)

Output

Aruba

Afghanistan

Angola

Anguilla

Åland Islands

Albania

Andorra

United Arab Emirates

Argentina

Armenia

….

….

….

Samoa

Yemen

South Africa

Zambia

Zimbabwe

Searching countries using the name

import pycountry

country_name = "India"

country = pycountry.countries.get(name=country_name)

print(country)

Output

Country(alpha_2='IN', alpha_3='IND', flag='????????', name='India', numeric='356', official_name='Republic of India')

Searching countries using the code

country_alpha2 = "IN"

country = pycountry.countries.get(alpha_2=country_alpha2)

print(country)

Output

Country(alpha_2='IN', alpha_3='IND', flag='????????', name='India', numeric='356', official_name='Republic of India')

This module has also a fuzzy search logic so if there is a spelling mistake this will search for similar names

Fuzzy search

query = "Indi"

countries = pycountry.countries.search_fuzzy(query)

Output

[Country(alpha_2='IN', alpha_3='IND', flag='????????', name='India', numeric='356', official_name='Republic of India'), Country(alpha_2='IO', alpha_3='IOT', flag='????????', name='British Indian Ocean Territory', numeric='086'), Country(alpha_2='GN', alpha_3='GIN', flag='????????', name='Guinea', numeric='324', official_name='Republic of Guinea'), Country(alpha_2='US', alpha_3='USA', flag='????????', name='United States', numeric='840', official_name='United States of America'), Country(alpha_2='TZ', alpha_3='TZA', common_name='Tanzania', flag='????????', name='Tanzania, United Republic of', numeric='834', official_name='United Republic of Tanzania'), Country(alpha_2='CO', alpha_3='COL', flag='????????', name='Colombia', numeric='170', official_name='Republic of Colombia'), Country(alpha_2='IT', alpha_3='ITA', flag='????????', name='Italy', numeric='380', official_name='Italian Republic'), Country(alpha_2='UG', alpha_3='UGA', flag='????????', name='Uganda', numeric='800', official_name='Republic of Uganda')]

Listing all the currencies

for currency in pycountry.currencies:
    print(currency.name)

Output

UAE Dirham

Afghani

Lek

Armenian Dram

Netherlands Antillean Guilder

Kwanza

Argentine Peso

….

….

….

Sucre

Codes specifically reserved for testing purposes

ADB Unit of Account

The codes assigned for transactions where no currency is involved

Yemeni Rial

Rand

Zambian Kwacha

Zimbabwe Dollar

 

Listing all languages

for language in pycountry.languages:
    print(language.name)

Output

Ghotuo

Alumu-Tesu

Ari

Amal

Arbëreshë Albanian

Aranadan

Ambrak

Abu' Arapesh

…

…

…

Yongbei Zhuang

Yang Zhuang

Youjiang Zhuang

Yongnan Zhuang

Zyphe Chin

Zaza

Zuojiang Zhuang

Note: Flag images are not rendering correctly in code section. here it shows ??????? this will be actual flag icon 

I hope you have learned something from this post. To know more about this package you can refer to the official documentation. Please share your valuable suggestions with afsal@parseltongue.co.in