I'm planning to build a contact web page where users can select their country and choose a specific region within that country. I need to have the corresponding calling code for each country. To achieve this, I would like to generate a Python dictionary that includes all the necessary information. Here's the desired format I'd like to achieve:
output:
COUNTRY_DATA = {
"COUNTRY_ID_1": {
"name": "COUNTRY_NAME_1",
"calling_code": COUNTRY_NUMBER_1,
"states": [LIST_COUNTRY_STATES_1]
},
"COUNTRY_ID_2": {
"name": "COUNTRY_NAME_2",
"calling_code": COUNTRY_NUMBER_2,
"states": [LIST_COUNTRY_STATES_2]
},
"COUNTRY_ID_3": {
"name": "COUNTRY_NAME_3",
"calling_code": COUNTRY_NUMBER_3,
"states": [LIST_COUNTRY_STATES_3]
},
...
}
Could you please assist me in creating the Python dictionary COUNTRY_DATA in the specified format?
Expected output:
COUNTRY_DATA = {
"US": {
"name": "United States",
"calling_code": "+1",
"states": ["California", "New York", "Texas"]
},
"GB": {
"name": "United Kingdom",
"calling_code": "+44",
"states": ["England", "Scotland", "Wales"]
},
"CA": {
"name": "Canada",
"calling_code": "+1",
"states": ["Ontario", "Quebec", "British Columbia"]
},
...
}
One of the possible solutions is to use python libraries: phonenumbers and pycountry.
The first example prints all values to the console, while the second example creates a python dictionary that can be copied into the code and used.
Example 1:
Example 2:
The output can be saved directly to a file: