Phone numbers - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Phone numbers (/thread-24959.html) |
Phone numbers - ovidius80 - Mar-12-2020 Hello everyone I am trying to use phone-iso3166, phonenumbers and pycountries to create on the fly dictionaries of countries with the corresponding international phone code. Can anyone give me an example where I will input a country and I get the international phone code? Or at least where can I see all the attributes so that I can experiment? I found this example <code> >>>from phone_iso3166.country import * >>>phone_country(45) 'DK' </code> can anyone tell me how to do the exact opposite? Give the two or three (even better for me) letter code for the country and get as output the international phone code? Thank you in advance for the help RE: Phone numbers - DeaD_EyE - Mar-12-2020
from phone_iso3166 import country country2number = country.country_prefixes() # exchanging keys with values number2country = {value: key for key, value in country2number.items()}The function country.phone_country() returns the alpha2 for the number prefix.The function country.country_prefix() does the inverse. There you get the number prefix of one country.If you do the trick with exchanging keys <-> values, then keep in mind, that repeating values are overwritten. The last occurrence wins. RE: Phone numbers - Larz60+ - Mar-12-2020 Also, if you want more that just country code, there's the CIA FWorld Factbook https://www.cia.gov/library/publications/resources/the-world-factbook/ from this page, you can select country (pulldown, upper right) and obtain a plethora of information about it. |