Skip to main content

A fork for pycountry, correct the country name of Taiwan

Project description

pycountry provides the ISO databases for the standards:






Subdivisions of countries





The package includes a copy from Debian’s pkg-isocodes and makes the data accessible through a Python API.

Translation files for the various strings are included as well.

Countries (ISO 3166)

Countries are accessible through a database object that is already configured upon import of pycountry and works as an iterable:

>>> import pycountry
>>> len(pycountry.countries)
>>> list(pycountry.countries)[0]
<pycountry.db.Country object at 0x...>

Specific countries can be looked up by their various codes and provide the information included in the standard as attributes:

>>> germany = pycountry.countries.get(alpha2='DE')
>>> germany
<pycountry.db.Country object at 0x...>
>>> germany.alpha2
>>> germany.alpha3
>>> germany.numeric
>>> germany.official_name
'Federal Republic of Germany'

Note that historic countries, defined by the ISO 3166-3 sub-standard are not included in this list.

Country subdivisions (ISO 3166-2)

The country subdivisions are a little more complex than the countries itself because they provide a nested and typed structure.

All subdivisons can be accessed directly:

>>> len(pycountry.subdivisions)
>>> list(pycountry.subdivisions)[0]
<pycountry.db.Subdivision object at 0x...>

Subdivisions can be accessed using their unique code and provide at least their code, name and type:

>>> de_st= pycountry.subdivisions.get(code='DE-ST')
>>> de_st.code
>>> de_st.type
<pycountry.db.Country object at 0x...>

Some subdivisions specify another subdivision as a parent:

>>> al_br = pycountry.subdivisions.get(code='AL-BU')
>>> al_br.code
>>> al_br.type
>>> al_br.parent_code
>>> al_br.parent
<pycountry.db.Subdivision object at 0x...>

The divisions of a single country can be queried using the country_code index:

>>> len(pycountry.subdivisions.get(country_code='DE'))
>>> len(pycountry.subdivisions.get(country_code='US'))

Scripts (ISO 15924)

Scripts are available from a database similar to the countries:

>>> len(pycountry.scripts)
>>> list(pycountry.scripts)[0]
<pycountry.db.Script object at 0x...>
>>> latin = pycountry.scripts.get(name='Latin')
>>> latin
<pycountry.db.Script object at 0x...>
>>> latin.alpha4
>>> latin.numeric

Currencies (ISO 4217)

The currencies database is, again, similar to the ones before:

>>> len(pycountry.currencies)
>>> list(pycountry.currencies)[0]
<pycountry.db.Currency object at 0x...>
>>> argentine_peso = pycountry.currencies.get(letter='ARS')
>>> argentine_peso
<pycountry.db.Currency object at 0x...>
>>> argentine_peso.letter
'Argentine Peso'
>>> argentine_peso.numeric

Languages (ISO 639)

The languages database is similar too:

>>> len(pycountry.languages)
>>> list(pycountry.languages)[0]
<pycountry.db.Language object at 0x...>
>>> aragonese = pycountry.languages.get(alpha2='an')
>>> aragonese.alpha2
>>> aragonese.bibliographic
>>> aragonese.terminology


Locales are available in the pycountry.LOCALES_DIR subdirectory of this package. The translation domains are called isoXXX according to the standard they provide translations for. The directory is structured in a way compatible to Python’s gettext module.

Here is an example translating language names:

>>> import gettext
>>> german = gettext.translation('iso3166', pycountry.LOCALES_DIR,
...                              languages=['de'])
>>> german.install()
>>> _('Germany')

Taiwan country name

The official name of Taiwan is Republic of China, but however, due to the pressure from Chinese government, ISO defines the name of Taiwan as Province of China, which is not true after all. Therefore, we replace Province of China as Republic of China in this fork, and it is what this fork mainly for. If you don’t agree with this modification, please just don’t use it.

>>> import gettext
>>> tw = pycountry.countries.get(alpha2='TW')
'Taiwan, Republic of China'
>>> zh_tw = gettext.translation('iso3166', pycountry.LOCALES_DIR,
...                             languages=['zh_TW'])
>>> zh_tw.install(unicode=True)
>>> _('Taiwan, Republic of China')


0.12.2 (2011-05-19)

  • Make lxml an optional dependency (2010-10-19)

  • Fix some locales bug

  • Add test for Taiwan country name in Readme.txt

  • Follow original project to use revision 770fa9cd603f90f9fb982b32fe6f45d253f1d33e.

0.12.1 (2010-04-21)

  • Remedy brown-bag release 0.12 which was missing all data files due to a bad interaction between the build system for the data and zest.releaeser’s full-release script.

0.12 (2010-04-20)

  • Follow Debian repository to git.

  • Upgrade data to revision 770fa9cd603f90f9fb982b32fe6f45d253f1d33e as requested by #5488 and others.

  • Reflect subdivision changes with how they reference their parents in the XML (they used to use space as a separator but now use a hyphen).

  • Refactor index building structures a bit.

  • Remove superfluous ‘code’ index from subdivision database. (Together with the data upgrade this also gets rid of all the annoying warnings as described in #6667).

  • Some light PEP 8 improvements.

0.11 (2009-03-03)

  • Updated Debian repository to r1752.

0.10 (2008-06-26)

  • Added support for country subdivisions (ISO 3166-2).


  • Initial release

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 (6.6 MB view hashes)

Uploaded Source

Supported by

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