Skip to content

Instantly share code, notes, and snippets.

@math280h
Last active August 16, 2020 00:45
Show Gist options
  • Select an option

  • Save math280h/fd5ab0aafed2c0aaec659acdc8c03948 to your computer and use it in GitHub Desktop.

Select an option

Save math280h/fd5ab0aafed2c0aaec659acdc8c03948 to your computer and use it in GitHub Desktop.
Create Country Phone Code CSV
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)
@math280h
Copy link
Author

Added ISO3 to the CSV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment