Remember the days when you used a physical globe or scanned through a large heavy book to find the name of different regions and countries? With advances in technology, especially the internet, all this information is now available at the click of a button.

But what if you wanted to take it a step further and find essential data about a country such as its capital, geographic coordinates, or currency? Look no further than Python’s amazing Country Info module, which can do the job in a matter of seconds.

What Is the CountryInfo Module?

Porimol Chandro developed the Country Info module. It has functions to fetch a country’s capital city, geographical coordinates, timezone, area, population, and more.

To install this module, open your terminal and execute:

        pip install countryinfo
    

You can also fetch the live weather data of any country or region and use it along with the information and enhance your dataset.

How to Use the Module’s Functions

You can fetch information using the different functions of the CountryInfo module as follows.

1. Getting the Alternate Names or Spellings of a Country

You can fetch the alternate names or spellings of a country like so:

        from countryinfo import CountryInfo
name = 'India'
country = CountryInfo(name)
data1 = country.alt_spellings()
print(data1)

Start by importing the CountryInfo class from the countryinfo module. You can then create an instance of CountryInfo by passing it a string containing the name of a country.

You can call various API methods on the CountryInfo object you’ve created. For example, alt_spellings() returns a list of alternate names of the country.

The Python snippet for getting the alternate names of a country produces the following output:

Alternate spellings of country

2. Getting the Capital and Its Geographic Coordinates

You can use capital() to get the capital city of any country. It returns a string representing the name of the capital:

        data2 = country.capital()
print(data2)

This Python snippet produces the following output:

Capital of country

Use the API method captial_latlng() to fetch the geographical coordinates of a country’s capital. This method returns a list containing two elements, each a floating point number:

        data3 = country.capital_latlng()
print(data3)

The Python snippet for getting the latitude and latitude of a country produces the following output:

Capital latitude and longitude

3. Getting the Area of a Country, Its Provinces, and the Surrounding Border Countries

You can use the API method, area() to get the area of any country in square kilometers. Implement the code as:

        data4 = country.area()
print(data4)

The Python snippet for getting the area of a country produces the following output:

Area of country

You can get a list of provinces of a country using provinces(). Each element in the list is a string representing a province. Implement the code as:

        data5 = country.provinces()
print(data5)

The Python snippet for getting the provinces of a country produces the following output:

Provinces of country

You can fetch the names of bordering countries using the API method, borders(). Each element in the list is a country code in ISO-3 format, a three-character uppercase string. Implement the code as:

        data6 = country.borders()
print(data6)

The Python snippet for getting the name of border countries produces the following output:

Borders of country

4. Getting the Calling Codes of a Country

You can fetch the international calling codes of a country using calling_codes(). This returns a list of strings, each containing the numeric telephone prefix you need to call the country from another:

        data7 = country.calling_codes()
print(data7)

The Python snippet for getting the calling codes of a country produces the following output:

Calling code of country

5. Getting Currency, Population, and Timezones

Fetch the official currency format of a country using the API method, currencies(). This method returns another list of strings:

        data8 = country.currencies()
print(data8)

The Python snippet for getting the currency of a country produces the following output:

Currency of country

Fetch the approximate number of people living in a country using population(). This method returns an int. Use it like this:

        data9 = country.population()
print(data9)

The Python snippet for getting the population of a country produces the following output:

Population of country

A country may have one or more than one time zone, depending on its size. You can fetch the different timezones of a country using the timezones() API method as:

        data10 = country.timezones()
print(data10)

The Python snippet for getting the different timezones of a country produces the following output:

Timezone of country

6. Getting the Region and Subregion of a Country

Fetch the general region or continent a country belongs to using the region() API method. Implement the code as:

        data11 = country.region()
print(data11)

The Python snippet for getting the region of a country produces the following output:

Region of a country

Similarly, you can a more specific region the country belongs to using subregion() as:

        data12 = country.subregion()
print(data12)

The Python snippet for getting the subregion of a country produces the following output:

Subregion of country

7. Getting the Wikipedia URL of a Country

Wikipedia is one of the best sources of information for anything on the internet. You can fetch the Wikipedia page of a country using the API method, wiki(). Implement the code as:

        data13 = country.wiki()
print(data13)

The Python snippet for getting the Wikipedia page of a country produces the following output:

Wiki page of country

8. Getting the Top Level Domain for a Country

A ccTLD is a country code top-level domain name that indicates a country or a geographical area of the website. It is usually two letters long such as .in for India and .ca for Canada. You can fetch the top-level domain for a country using tld() as:

        data14 = country.tld()
print(data14)

The Python snippet for getting the top-level domain of a country produces the following output:

Top Level Domain of country

9. Getting All the Available Information About a Country

You can fetch all the available information of a country using the API method, info(). It returns a dictionary of key/name values, which you can iterate over using a for loop:

        data15 = country.info()

for key, value in data15.items():
    print(key, ":", value)

The Python snippet for getting all the available information about a country produces the following output:

Info of country

10. Getting the Information of All the Countries and Creating a Dataset

The CountryInfo module provides an API method called all() that returns a dictionary containing all the information for all the countries in the world:

        from countryinfo import CountryInfo
import pandas as pd
country = CountryInfo()
data = country.all()

You can use the pandas module to manipulate this data. The pandas module provides many commands for working with DataFrames, including one to save data in CSV format:

        df = pd.DataFrame(data)
df.to_csv("Country_Data_Fetcher.csv")

Fetching Data From Websites

While this handy Python module provides all the essential information of any country, fetching clean data is not an easy task. Knowledge of web scraping will help you fetch data from any website on the internet. You can then analyze and use that data in your applications.

Python has a powerful module called BeautifulSoup that you can install to perform web scraping and save hours of your work.