Skip to main content

A Python package that provides information about countries, including their name, nationality, and other relevant data. Perfect for geographic data applications.

Project description

pycountryinfo

pycountryinfo is a Python package that provides detailed, preloaded information about countries worldwide. The package comes with up-to-date data sourced from the REST Countries API (and enhanced by us) to ensure accuracy and comprehensiveness.

Installation

pip install pycountryinfo

Requirements

  • Python 3.7 or higher

Properties Available for a Country:

  • common_name: The common name of the country (e.g., 'Ghana').
  • official_name: The official name of the country (e.g., 'Republic of Ghana').
  • iso_codes: The ISO codes associated with the country (e.g., {'alpha_2': 'GH', 'alpha_3': 'GHA', 'numeric': '288'}).
  • region: The region the country belongs to (e.g., 'Africa').
  • subregion: The subregion the country belongs to (e.g., 'Western Africa').
  • capital: The capital city of the country (e.g., 'Accra').
  • languages: A list of languages spoken in the country (e.g., ['English']).
  • population: The population of the country (e.g., 31072945).
  • area: The total area of the country in square kilometers (e.g., 238533.0).
  • currency: The currency used in the country (e.g., {'name': 'Ghanaian cedi', 'code': 'GHS', 'symbol': '₵'}).
  • timezones: A list of timezones the country falls under (e.g., ['UTC']).
  • borders: A list of countries that share borders with this country (e.g., ['BFA', 'CIV', 'TGO']).
  • nationality: The nationality associated with the country (e.g., 'Ghanaian').
  • flag: The URL of the country's flag image (e.g., 'https://flagcdn.com/w320/gh.png').
  • translations: Translations of the country's name in different languages
  • latlng: The latitude and longitude of the country (e.g., [8.0, -2.0]).
  • gini_index: The Gini index for income inequality in the country (e.g., {'2016': 43.5}).
  • independent: A boolean indicating whether the country is independent (e.g., True).
  • calling_code: The international calling code for the country (e.g., '+233').
  • alt_spellings: Alternative spellings or names for the country (e.g., ['GH']).
  • wiki: The URL for the country's Wikipedia page (e.g., '').
  • provinces: A list of provinces or regions within the country (e.g., ['Ashanti', 'Ahafo', 'Bono', ...]).
  • area_geometry: The geometric representation of the country's area (e.g., {'type': 'Polygon', 'coordinates': [[ [1.060122, 5.928837], [-0.507638, 5.343473], ...]]}).

Example:

Here’s a quick example on how you might retrieve one of these properties:

# Initialize the PyCountryInfo object for Ghana
pycountryinfo = PyCountryInfo('Ghana')

# Accessing country data
print(pycountryinfo.country_data['common_name'])  # Output: Ghana
print(pycountryinfo.country_data['official_name'])  # Output: Republic of Ghana
print(pycountryinfo.country_data['capital'])  # Output: Accra
print(pycountryinfo.country_data['region'])  # Output: Africa
print(pycountryinfo.country_data['subregion'])  # Output: Western Africa
print(pycountryinfo.country_data['languages'])  # Output: ['English']
print(pycountryinfo.country_data['population'])  # Output: 31072940
print(pycountryinfo.country_data['area'])  # Output: 238533
print(pycountryinfo.country_data['timezones'])  # Output: ['UTC']
print(pycountryinfo.country_data['alt_spellings'])  # Output: ['GH', 'GHA', 'Gold Coast']
print(pycountryinfo.country_data['calling_code'])  # Output: '+233'
print(pycountryinfo.country_data['translations'])  # Output: {'de': 'Ghana', 'es': 'Ghana', 'fr': 'Ghana', ...}
print(pycountryinfo.country_data['independent'])  # Output: True
print(pycountryinfo.country_data['latlng'])  # Output: [7.9465, -1.0232]
print(pycountryinfo.country_data['nationality'])  # Output: 'Ghanaian'
print(pycountryinfo.country_data['provinces'])  # Output: ['Ashanti', 'Ahafo', 'Bono', ...]

Methods

get_nationality(self, country:str) -> str Gets the nationality of the specified country.

  • country: (str) The name of the country.

get_nationalities(self) -> Tuple[str, ...] Returns a tuple of all nationalities available in the countries data.

is_valid_country_nationality(self, country:str, nationality:str) -> bool Checks if the given nationality is valid for the specified country.

  • country: (str) The name of the country.
  • nationality: (str) The nationality to validate.

is_valid_nationality(self, nationality:str) -> bool Checks if the given nationality is valid across all countries.

  • nationality: (str) The nationality to validate.

get_country_from_nationality(self, nationality:str) -> str Gets the country associated with a given nationality.

  • nationality: (str) The nationality to get the country for.

get_countries(self) -> Tuple[str, ...] Returns a tuple of all country names available in the data.

is_valid_country(self, country:str) -> bool Checks if the given country name exists in the data.

  • country: (str) The country name to validate.

get_provinces(self, country:str) -> List[str] Gets a list of provinces for a given country.

  • country: (str) The name of the country to get provinces for.

is_valid_country_province(self, country:str, province:str) -> bool Checks if a province is valid for a specified country.

  • country: (str) The name of the country.
  • province: (str) The province name to validate.

Example Method Usage

from pycountryinfo.country_info import PyCountryInfo

# Initialize the PyCountryInfo object
country_info = PyCountryInfo(country_name_type='common')

# Get a country's nationality
nationality = country_info.get_nationality('Ghana')
print(nationality)  # Output: 'Ghanaian'

# Get all nationalities
nationalities = country_info.get_nationalities()
print(nationalities)  # Output: ('American', 'Canadian', 'Ghana', ...)

# Get a country's provinces (for example, Ghana)
provinces = country_info.get_provinces('Ghana')
print(provinces)  # Output: ['Ashanti', 'Ahafo', Bono, ...]

# Check if a country is valid
is_valid = country_info.is_valid_country('Ghana')
print(is_valid)  # Output: True

# Get a country from a nationality
country_from_nationality = country_info.get_country_from_nationality('Ghanaian')
print(country_from_nationality)  # Output: 'Ghana'

Contributing

If you'd like to contribute to this package, feel free to fork the repository, make your changes, and submit a pull request.

License

This project is licensed under the MIT License.

Contact

If you have questions or suggestions, feel free to open an issue or contact nyamejustice2000@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycountryinfo-1.0.7.tar.gz (322.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycountryinfo-1.0.7-py3-none-any.whl (343.0 kB view details)

Uploaded Python 3

File details

Details for the file pycountryinfo-1.0.7.tar.gz.

File metadata

  • Download URL: pycountryinfo-1.0.7.tar.gz
  • Upload date:
  • Size: 322.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for pycountryinfo-1.0.7.tar.gz
Algorithm Hash digest
SHA256 06c8ebe2bbf5e997d43c2f64775df5a074ce11799e9c84d9da7bf78d25fb1737
MD5 bfe3f1bf390707ae46ba305aa74daaa2
BLAKE2b-256 2bf9fd99e2f55e2dccfcd90f63d434973fa641e1dfc0cf375de2f6ba8b792ec6

See more details on using hashes here.

File details

Details for the file pycountryinfo-1.0.7-py3-none-any.whl.

File metadata

  • Download URL: pycountryinfo-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 343.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for pycountryinfo-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c7c356a2327aa601b3e00cd09662bda54fbe84891ba35a0804d55eaf5c202e6f
MD5 33f7fed4208296a69d5421b41f4c4cca
BLAKE2b-256 cc1116daba714a04606bb0b83dcee609dc7dbb17a0f96133c9786472c81a7cac

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page