Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Phone numbers
#1
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
Reply
#2
Output:
Signature: phone_iso3166.country.country_prefixes() Docstring: Function that return a dictionary, with an ISO-3166-1 alpha-2 code, as the key, and the country prefix as the value. For countries with multiple prefixes, an abitrary one of them is chosen, for United states and Canada code is denoted as 1. File: c:\users\admin\appdata\local\programs\python\python38\lib\site-packages\phone_iso3166\country.py Type: function
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Also, if you want more that just country code, there's the CIA FWorld Factbook https://www.cia.gov/library/publications...-factbook/
from this page, you can select country (pulldown, upper right) and obtain a plethora of information about it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Microsoft text phone verifying account cito 2 947 Jul-21-2022, 12:16 PM
Last Post: cito
  Send SMS from my phone number aster 3 2,684 Jul-03-2021, 02:34 PM
Last Post: ndc85430
  lists Name Address Phone Heyjoe 2 1,751 Jun-21-2020, 10:47 AM
Last Post: Heyjoe
  Access phone via Bluetooth maxwell 2 2,233 Jun-03-2020, 05:22 PM
Last Post: maxwell
  Phone Number metro17 3 2,579 Aug-22-2019, 09:20 AM
Last Post: perfringo
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,667 May-09-2019, 12:19 PM
Last Post: Pleiades
  Phone app from python code Androsmeq 1 2,208 Mar-29-2019, 05:16 PM
Last Post: gruntfutuk
  Trying to move a RGB ledstrip with slider of my phone in almost "realtime" help Edris89 0 1,932 Sep-28-2018, 11:03 PM
Last Post: Edris89

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020