The country converter (coco) is a Python package designed to convert and match country names between various classification schemes and different naming conventions. It leverages regular expressions for robust matching and supports a wide array of classifications, including ISO, UN, EU, OECD, FIFA, IOC, continents, and several MRIO/IAM databases. The current version is 1.3.2, and it typically sees several updates per year to enhance classifications and features.
pip install country-converterVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the `country_converter` library and use its `convert` function to transform country names between different classification schemes like ISO2, ISO3, and various continent classifications.
Ensure any custom country data provided to `country-converter` includes an 'ISO2' column with appropriate values for matching, especially if upgrading from versions prior to 0.7.6.
Monitor the Python logging output for `country_converter` warnings, or explicitly handle `None` (or the original unmatched input) in the results. For finer control, you can configure the logging behavior or specify `not_found='keep'` or `not_found='raise'` in the `convert` method to either retain the original input or raise an error.
For ambiguous country names or when certainty is required, explicitly specify the source classification using the `src` parameter in the `convert` method (e.g., `coco.convert(names='US', src='ISO2', to='ISO3')`).
Install the package using pip: `pip install country-converter`
First, instantiate the `CountryConverter` class, then call the `convert` method on that instance: ```python import country_converter as coco cc = coco.CountryConverter() countries = ['Germany', 'France'] converted_countries = cc.convert(names=countries, to='ISO3') ```
Verify the spelling and format of the input country names/codes. Ensure they are standard names or codes recognized by common classification schemes (e.g., ISO2, ISO3, UN numeric). If the input format is known, specify the `src` parameter (e.g., `src='name_short'`). You can also set `not_found=None` to return `None` for unmatchable entries instead of raising an error or keeping the original name.
Split the string containing multiple country names into a list of individual country names before passing it to `country_converter`. For example:
```python
import country_converter as coco
cc = coco.CountryConverter()
country_string = 'United States, Canada, Mexico'
country_list = [c.strip() for c in country_string.split(',')]
converted_names = cc.convert(names=country_list, to='ISO3')
```