{"id":17563977,"url":"https://github.com/econcz/pyconvertu","last_synced_at":"2025-03-29T12:14:56.323Z","repository":{"id":62579467,"uuid":"371825256","full_name":"econcz/pyconvertu","owner":"econcz","description":"Tools for creation or conversion of lists from/to desired classification (the default is ISO 3166-1)","archived":false,"fork":false,"pushed_at":"2024-02-22T13:44:15.000Z","size":42,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-24T11:11:26.776Z","etag":null,"topics":["alpha-2","alpha-3","classification","english","french","iso-3166","numeric","pypi","python3","regular-expressions"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/econcz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-28T21:47:46.000Z","updated_at":"2024-02-22T13:43:01.000Z","dependencies_parsed_at":"2024-10-22T12:23:37.605Z","dependency_job_id":null,"html_url":"https://github.com/econcz/pyconvertu","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/econcz%2Fpyconvertu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/econcz%2Fpyconvertu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/econcz%2Fpyconvertu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/econcz%2Fpyconvertu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/econcz","download_url":"https://codeload.github.com/econcz/pyconvertu/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246180923,"owners_count":20736460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alpha-2","alpha-3","classification","english","french","iso-3166","numeric","pypi","python3","regular-expressions"],"created_at":"2024-10-21T13:10:41.515Z","updated_at":"2025-03-29T12:14:56.299Z","avatar_url":"https://github.com/econcz.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tools for creation and conversion from/to desired classification (the default is ISO 3166-1)\n\n\nThis project was inspired by **[pycountry](https://pypi.org/project/pycountry/)** and  **[pycountry-convert](https://pypi.org/project/pycountry-convert/)** modules and is a port of the **Stata package** **[pyconvertu](https://ideas.repec.org/c/boc/bocode/s458892.html)** *(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](https://pypi.org/project/pandas/)** to process **pandas.DataFrame()**, `data`, `index`, and / or `columns` (consult examples):\n\n- ``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**.  \n- ``classification(source_file=None, from_classification='')`` returns a **list** created from a classification.  \n- ``info(source_file=None)`` prints metadata and sources from the built-in or user-defined JSON file.\n\n\n## Parameters:\n- `source_file` : *raw str* or *unicode*, optional.  \nRelative or absolute path to the user-defined JSON file.\n- `from_list` : *sequence* of *iterable*.  \nInput data.\n- `to_classification` : *str* or *unicode*.  \n'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).\n- `from_classification` : *str* or *unicode*.  \n'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).\n\n\n`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](https://docs.python.org/3/library/json.html)** module:\n```\n[\n    {\n        \"regex\":    \"^(.*afgh.*|\\\\s*AFG\\\\s*|\\\\s*AF\\\\s*|\\\\s*4\\\\s*)$\",\n        \"name_en\":  \"Afghanistan\",                  # classification A\n        \"name_fr\":  \"Afghanistan (l')\",             # classification B\n        \"iso3\":     \"AFG\",                          # ...\n        \"iso2\":     \"AF\",\n        \"isoN\":     \"4\"\n    },\n    ...\n    {\n        \"metadata\": {\n            \"name_en\": \"English short name\",\n            \"name_fr\": \"French short name\",\n            \"iso3\": \"alpha-3 code\",\n            \"iso2\": \"alpha-2 code\",\n            \"isoN\": \"numeric\"\n        }\n    },\n    {\n        \"sources\": [\n            \"[https://www.iso.org/iso-3166-country-codes.html](ISO 3166 COUNTRY CODES)\",\n            \"[https://en.wikipedia.org/wiki/List_of_alternative_country_names](ALTERNATIVE NAMES)\"\n        ]\n    }\n]\n```\n\n\n## Returns:\n- `l` : *list*.  \nProcessed data.\n\n\n## Examples:\n```\nimport pandas as pd\nfrom pyconvertu import convert\nfrom pyconvertu import classification\nfrom pyconvertu import info\n\n# Create a pandas dataframe with ISO 3166-1 alpha-3 as `index'\ndata = [1, 2, 3, 4, 5, 6, 7]\niso3 = convert(\n    from_list=['Canada', 'France', 'Germany', 'Italy', 'Japan', 'United Kingdom', 'United States'],\n    to_classification='iso3'\n)\npd.DataFrame(data, index=iso3)\n\n# Create a pandas dataframe from available classifications\ndf = pd.DataFrame()\ndf['iso3'] = classification(from_classification='iso3')\nfor s in ['iso2', 'isoN', 'name_en', 'name_fr']:\n    df[s] = convert(\n        from_list=df['iso3'],\n        to_classification=s\n    )\nprint(df)\n\n# Print information and metadata for the built-in JSON file and my_file.json\ninfo()\ninfo(source_file=r'my_file.json')\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feconcz%2Fpyconvertu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feconcz%2Fpyconvertu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feconcz%2Fpyconvertu/lists"}