Tools for creation or conversion of lists from/to desired classification (the default is ISO 3166-1)
Project description
Tools for creation and conversion from/to desired classification (the default is ISO 3166-1)
This project was inspired by pycountry and pycountry-convert modules and is a port of the Stata package pyconvertu (Stata module to convert a string variable into a classification from the default or user-provided JSON file with the help of Python 3) written in Python 3 and ADO. The tools can, for example, be used together with pandas to process pandas.DataFrame(), data
, index
, and / or columns
(consult examples):
convert(source_file=None, from_list=[], to_classification='')
converts a tuple or a list into a classification from a built-in or user-defined JSON file using regular expressions.classification(source_file=None, from_classification='')
returns a list created from a classification.info(source_file=None)
prints metadata and sources from the built-in or user-defined JSON file.
Parameters:
source_file
: raw str or unicode, optional.
Relative or absolute path to the user-defined JSON file.from_list
: sequence of iterable.
Input data.to_classification
: str or unicode.
'name_en' (English name), 'name_fr' (French name), 'iso3' (ISO 3166-1 alpha-3), 'iso2' (ISO 3166-1 alpha-2), or 'isoN' (ISO 3166-1 numeric).from_classification
: str or unicode.
'name_en' (English name), 'name_fr' (French name), 'iso3' (ISO 3166-1 alpha-3), 'iso2' (ISO 3166-1 alpha-2), or 'isoN' (ISO 3166-1 numeric).
source_file
(if defined) replaces the default classification (ISO 3166-1). The file must contain a list of dictionaries where regex
is a compulsory key in each one. The default JSON file was prepared with the help of json module:
[
{
"regex": "^(.*afgh.*|\\s*AFG\\s*|\\s*AF\\s*|\\s*4\\s*)$",
"name_en": "Afghanistan", # classification A
"name_fr": "Afghanistan (l')", # classification B
"iso3": "AFG", # ...
"iso2": "AF",
"isoN": "4"
},
...
{
"metadata": {
"name_en": "English short name",
"name_fr": "French short name",
"iso3": "alpha-3 code",
"iso2": "alpha-2 code",
"isoN": "numeric"
}
},
{
"sources": [
"[https://www.iso.org/iso-3166-country-codes.html](ISO 3166 COUNTRY CODES)",
"[https://en.wikipedia.org/wiki/List_of_alternative_country_names](ALTERNATIVE NAMES)"
]
}
]
Returns:
l
: list.
Processed data.
Examples:
import pandas as pd
from pyconvertu import convert
from pyconvertu import classification
from pyconvertu import info
# Create a pandas dataframe with ISO 3166-1 alpha-3 as `index'
data = [1, 2, 3, 4, 5, 6, 7]
iso3 = convert(
from_list=['Canada', 'France', 'Germany', 'Italy', 'Japan', 'United Kingdom', 'United States'],
to_classification='iso3'
)
pd.DataFrame(data, index=iso3)
# Create a pandas dataframe from available classifications
df = pd.DataFrame()
df['iso3'] = classification(from_classification='iso3')
for s in ['iso2', 'isoN', 'name_en', 'name_fr']:
df[s] = convert(
from_list=df['iso3'],
to_classification=s
)
print(df)
# Print information and metadata for the built-in JSON file and my_file.json
info()
info(source_file=r'my_file.json')
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
File details
Details for the file pyconvertu-0.4.1.tar.gz
.
File metadata
- Download URL: pyconvertu-0.4.1.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a127b1f0b1c2536b13dbf61ad429e500bbf8d32067292b7a46f6181696b9088 |
|
MD5 | 4a3ed0bd4e42bb0637be24537ca4478a |
|
BLAKE2b-256 | c6e75ff22ea8cccc527ad56d8659849137d63b856507d5dbd9c13371b03789ea |