A Python package to access the most up-to-date and accurate info about countries and their associated subdivisons using the ISO3166-2 standard.
Project description
ISO3166-2
Custom-built Python wrapper for RestCountries API (https://restcountries.com/) which includes an abundance of information about all ISO3166 countries. But this package also includes information about all countrys' ISO 3166-2 subdivision codes & names, which is absent from RestCountries. Available via a Python software package; a demo is available colab.
Table of Contents
Introduction
iso3166-2 is a custom-built Python wrapper for RestCountries (https://restcountries.com/) API which includes an abundance of information about all ISO3166 countries. But this package also includes information about all countrys' ISO 3166-2 subdivision codes & names, which is absent from RestCountries. The International Organisation for Standards defines codes for the names of countries, dependent territories, special areas of geographical interest, and their principal subdivisions [1]. This repo focuses on the ISO 3166-2 standard.
The ISO 3166-2 defines codes for identifying the principal subdivisions (e.g., provinces, states, municipality etc) of all countries coded in ISO 3166-1. The official name of the standard is "Codes for the representation of names of countries and their subdivisions – Part 2: Country subdivision code." It was first published in 1998 [2]. As of 29 November 2022 there are 5,043 codes defined in ISO 3166-2. For some countries, codes are defined for more than one level of subdivisions.
Latest Updates
An important thing to note about the ISO 3166-2 and its subdivision codes/names is that changes are made consistently to it, from a small subdivision name change to an addition/deletion of a whole subdivision. Therefore, it's important that this library and its JSONs have the most up to date data. To achieve this, the iso3166-updates repo was created.
The iso3166-updates repo is another software pacakge and accompanying API that pulls the latest updates and changes for any and all countries in the ISO3166. The API is called every few months to check for any updates, which will then be manually incorporated into this repo. Similarly, the getISO3166_2.py script is called regularly to check for any updates for all country data using the restcountries API.
Installaion
Install the latest version of iso3166-2
via PyPi using pip:
pip3 install iso3166-2 --upgrade
Installation from source:
git clone -b master https://github.com/amckenna41/iso3166-2.git
cd iso3166_2
python3 setup.py install
Requirements
Usage
There are two main JSONs that iso3166-2
utilises, iso3166-2.json and iso3166-2-min.json. The first JSON contains all country information, including all data pulled from the restcountries API as well as the country's subdivision data, this file is 3.4 MB. The iso3166-2-min.json file is a minimised version of the first JSON, only containing each country's ISO3166-2 data, this file is 1.6 MB. In the main module iso3166_2.py, all data from the iso3166-2.json is accessible via the iso.country
object and all data from the iso3166-2-min.json is accessible via the iso.subdivisions
object.
The script getISO3166_2.py
is used for gathering and exporting all country and subdivision data to the mentioned JSONs. It uses the restcountries api and pycountry package to assemble all of the data together. To download all of the latest ISO 3166-2 subdivision data run the getISO3166_2.py
in a terminal or cmd, the script takes around 2 hours to execute (the script requires the additional pacakges: pycountry, tqdm and googlemaps):
python3 getISO3166_2.py --json_filename=iso3166_2.json --output_folder=iso3166_2
--json_filename: output filename for exported JSONs.
--output_folder: output folder to store JSONs.
Import ISO3166_2 class and access the country and subdivision data:
import iso3166_2 as iso
#access all country data
canada_iso3166_2 = iso.country["CA"]
denmark_iso3166_2 = iso.country["DK"]
estonia_iso3166_2 = iso.country["EE"]
fiji_iso3166_2 = iso.country["FJ"]
#access all country subdivision data
canada_iso3166_2 = iso.subdivisions["CA"]
denmark_iso3166_2 = iso.subdivisions["DK"]
estonia_iso3166_2 = iso.subdivisions["EE"]
fiji_iso3166_2 = iso.subdivisions["FJ"]
Get country data:
import iso3166_2 as iso
canada_iso3166_2.name #country name
denmark_iso3166_2.currencies #country currencies
estonia_iso3166_2.capital #country capital
fiji_iso3166_2.population #country population
Get a specific subdivision's info:
import iso3166_2 as iso
canada_iso3166_2.subdivisions['CA-AB'] #Alberta subdivision
denmark_iso3166_2.subdivisions['DK-81'] #Nordjylland subdivision
estonia_iso3166_2.subdivisions['EE-899'] #Viljandi subdivision
fiji_iso3166_2.subdivisions['FJ-03'] #Cakaudrove subdivision
Attributes
You can check the ATTRIBUTES.md file to get a description for each attribute/field in the JSIN exports.
Issues or Contributing
Any issues, errors or bugs can be raised via the Issues tab in the repository. Due to the nature of the ISO consistently updating the ISO 3166-2 codes/names every year the data in the JSONs may slightly lag behind these changes. My iso3166-updates repo was created to check for these updates periodically and implement them in the relevant repo's. Although, if you notice any out of date or missing subdivision info then please similarly raise an Issue in the Issues tab.
Contact
If you have any questions or comments, please contact amckenna41@qub.ac.uk.
References
[1]: https://en.wikipedia.org/wiki/ISO_3166
[2]: https://en.wikipedia.org/wiki/ISO_3166-2
Support
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for iso3166_2-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbbd85b88a9c9c0c29d521a6df6b0e91ced418aaa5ce5252b6a4fbf6f2628c78 |
|
MD5 | 0b3df24e36484b3d4685e6c135bf717a |
|
BLAKE2b-256 | 6b10a39e7acc0ab313ac35fbc0fbeb74eceb4783634f51b98c2acf65cb57a4f5 |