Skip to main content

Package to get country data from country names.

Project description

Country Regex Package

countre is a package with functions to get standardised names or codes from country names, as well as additional country information. Country names often differ between data sources, therefore having standardised codes for each country makes it easy to merge data from multiple sources.

The countries included are those defined by the International Organization for Standardization. The list can be found here. The sources of the country data can be found here.


Installation

countre can be installed via pip from PyPi:

pip install countre

Functions

countre.country_info(countries, attributes, no_match='no_match')

     Parameters:
          countries : iterable
              Country names, ISO 3166-1 alpha-2 codes, or ISO 3166-1 alpha-3 codes.
          variables : countre.enums.Attribute | list{countre.enums.Attribute}
              Specify either a single attribute or a list containing multiple attributes.
          no_match : str, default 'no match'
              String returned for a country if no match is found.
    Returns:
          A list of values if only one attribute is given. A dictionary if more than
          one variable is given. The dictionary keys are the attribute names and the
          values are lists of values.         

Note: The country_info function finds data via regex pattern matching for each unique country in the countries iterable and then returns the respective value for each element of countries. This means there is relatively little difference in performace between a list of unique countries and that same list repeated multiple times.


countre.member_countries(organisation, attribute=countre.enums.Attribute.COUNTRY)

     Parameters:
          organisation : countre.enums.Organisation
              Specify the organisation to get members of. Select from EU, EU_EEA, OECD, and OPEC.
          attrribute : countre.enums.Attribute
              To get member country names or ISO codes, specify COUNTRY, COUNTRY_SHORT, ISO2 or ISO3.
    Returns:
          List of either country names, ISO 3166-1 alpha-2 codes or ISO 3166-1 alpha-3
          codes for the members of the specified organisation.


Examples

>>> import countre
>>> from countre.enums import Attribute, Organisation
>>>
>>> countries = ['Australia', 'Germany', 'Sweden']
>>> attributes = [Attribute.ISO3, Attribute.CAPITAL, Attribute.CURRENCY_NAME]

Get a single attribute for a list of countries

>>> countre.country_info(countries=countries, attributes=Attribute.ISO3)

['AUS', 'DEU', 'SWE']

Get multiple attributes for a list of countries

>>> countre.country_info(countries=countries, attributes=attributes)

{
    'iso3': ['AUS', 'DEU', 'SWE'],
    'capital_city': ['Canberra', 'Berlin', 'Stockholm'],
    'currency_name': ['Australian dollar', 'Euro', 'Swedish krona']
}

When using multiple attributes, the output can easily be converted to a Pandas DataFrame

>>> import pandas as pd
>>> pd.DataFrame(countre.country_info(countries, attributes))

   iso3   country     capital_city
0  AUS    Australia   Canberra
1  DEU    Germany     Berlin
2  SWE    Sweden      Stockholm

Get organisation member countries. Not specifying an attribute will return the country names.

>>> countre.member_countries(Organisation.EU)

['Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', ...

Get organisation member countries with attribute specification

>>> countre.member_countries(Organisation.EU, Attribute.ISO3)

['AUT', 'BEL', 'BGR', 'HRV', 'CYP', ...

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

countre-0.0.5.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

countre-0.0.5-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file countre-0.0.5.tar.gz.

File metadata

  • Download URL: countre-0.0.5.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for countre-0.0.5.tar.gz
Algorithm Hash digest
SHA256 8685af51d74c7e5712825b3e21680959162a7253788484adb274abca4e947b23
MD5 761696a9292fb13415095316a8a784ff
BLAKE2b-256 6a44a853edb982fa2d6ab347e86c4f4f7ab144674fb783c875df768d1e64ba46

See more details on using hashes here.

File details

Details for the file countre-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: countre-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 3.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for countre-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d5efdc8de332e5f875f70cb8bf4fd5f45b4273226431bdb91d4715c39748ba6f
MD5 529be8268547083feaf832f361a339fd
BLAKE2b-256 9a562a24bd4ffd598238c9be0850f5bed69a984dd07adc985f4c2e8ef0da1b94

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