Last active
August 16, 2020 00:45
-
-
Save math280h/fd5ab0aafed2c0aaec659acdc8c03948 to your computer and use it in GitHub Desktop.
Create Country Phone Code CSV
This file contains hidden or 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 requests | |
| import json | |
| import csv | |
| # Gather Codes & Names | |
| print("Gathering Data") | |
| country_codes = requests.get("http://country.io/phone.json") | |
| country_names = requests.get("http://country.io/names.json") | |
| country_iso = requests.get("http://country.io/iso3.json") | |
| # Store JSON Data | |
| cc_data = country_codes.json() | |
| names_data = country_names.json() | |
| iso_data = country_iso.json() | |
| country_list = [] | |
| # Combine the two lists | |
| for country in cc_data: | |
| country_list.append({"name": names_data[country], "short": country, "country_code": cc_data[country], "iso": iso_data[country]}) | |
| print("Added:",names_data[country] + " -", country + ":", cc_data[country]) | |
| # Write CSV File | |
| with open('country_list.csv', mode='w+', newline='') as csvFile: | |
| country_csv = csv.writer(csvFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) | |
| for country in country_list: | |
| country_code = ''.join(e for e in country['country_code'] if e.isalnum()) | |
| if country_code != '' and country_code.isnumeric(): | |
| country_csv.writerow([country['name'], country['short'], int(country_code), country["iso"]]) | |
| else: | |
| print("Failed:", country) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added ISO3 to the CSV.