Skip to content

commondata-net/commondata-countries-python

Repository files navigation

commondata-countries

PyPI - License PyPI - Python Version

Work with ISO 3166-1 alpha2, alpha3 and numeric standard country data.

List, lookup with fuzzy search, and synonyms.

Installation

pip install commondata-countries

Usage

Iterate over all countries:

from commondata_countries import CountryData

countries = CountryData()

for country in countries:
    print(country.name)

List all countries:

from commondata_countries import CountryData

countries = CountryData()

print(countries.all())

Lookup a country

from commondata_countries import CountryData

countries = CountryData()

# Lookup by name (case insensitive, fuzzy search)
country = countries["Untied States of America"]
# Equivalent to
country = countries.get("Untied States of America")

# Lookup by ISO Alpha-2
country = countries["US"]
# Equivalent to
country = countries.get("US")

# Lookup by ISO Alpha-3
country = countries["USA"]
# Equivalent to
country = countries.get("USA")

# Lookup by ISO Numeric
country = countries[840]
# Equivalent to
country = countries.get(840)

# Lookup by synonym
country = countries["United States"]

# Search matching countries (ordered by best match first)
countries = countries.search("United States")

# Look up with fuzzy search
country = countries["United Stat"]

print(country)
> Country(name='United States of America', iso_alpha2='US', iso_alpha3='USA', iso_numeric=840)

Use CLI to lookup a country

python -m commondata-countries United States

Load countries data into pandas dataframe

import pandas as pd

from commondata_countries.data import countries

df = pd.DataFrame(countries)

Other Formats and Datasets

Download CSV, XLSX, JSON and YAML files from commondata.net/countries.

commondata.net maintains a collection of essential datasets in a variety of formats, including python bindings. Check out the full library here: commondata.net/library.

Contributing

Contributions are welcome! Please open an issue or submit a pull request here.

License

This project is licensed under GPLv3. See the LICENSE file for details.

Support

For feedback, feature requests, or support, please email support@commondata.net.

About

Python package to work with ISO 3166-1 alpha2 alpha3 and numeric standard country data

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages