Created
October 29, 2024 12:46
-
-
Save jamespacileo/46a8d4e7ee1480ded3a4b6282818354a to your computer and use it in GitHub Desktop.
setup_country_names.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib.request | |
import re | |
def main(): | |
url = 'https://raw.githubusercontent.com/mledoze/countries/refs/heads/master/countries.json' | |
response = urllib.request.urlopen(url) | |
data = json.loads(response.read().decode()) | |
mapping = {} | |
for country in data: | |
common_name = country['name']['common'] | |
official_name = country['name']['official'] | |
native_names = country['name']['native'] | |
# Collect all possible names | |
names = set() | |
names.add(common_name) | |
names.add(official_name) | |
# for lang in native_names.values(): | |
# names.add(lang['common']) | |
# names.add(lang['official']) | |
# Process names: remove "(the)" and trim whitespace | |
for name in names: | |
processed_name = re.sub(r'\s*\(the\)\s*', '', name, flags=re.IGNORECASE).strip() | |
mapping[processed_name] = common_name | |
# Save the mapping to a JSON file | |
with open('country-names.json', 'w', encoding='utf-8') as f: | |
json.dump(mapping, f, ensure_ascii=False, indent=2) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment