phonenumbers Python Library
This is a Python port of Google’s lib phonenumber library It supports Python 2.5-2.7 and Python 3.x (in the same codebase, with no 2to3 conversion needed).
Installation in pip
pip install phonenumbers
Installation in conda
to install phonenumber library in anaconda try running any of these commands in anaconda prompt
conda install -c conda-forge phonenumbers
conda install -c conda-forge/label/gcc7 phonenumbers
conda install -c conda-forge/label/cf201901 phonenumbers
conda install -c conda-forge/label/cf202003 phonenumbers
Example of usage of this library
Main object of this library is PhoneNumber
object. We can create this object from a string that contains phone number using the parse function. We have to provide the country code along with phone number
import phonenumbers
from phonenumbers import carrier
from phonenumbers import timezone
x = phonenumbers.parse("+442083661177", None)
print(x)
Country Code: 44 National Number: 2083661177
type(x)
<class 'phonenumbers.phonenumber.PhoneNumber'>
y = phonenumbers.parse("020 8366 1177", "GB")
print(y)
Country Code: 44 National Number: 2083661177 Leading Zero: False
You might want to get some information about the location that corresponds to a phone number. The geocoder.area_description_for_number
does this, when possible.
>>> from phonenumbers import geocoder
>>> ch_number = phonenumbers.parse("0431234567", "CH")
>>> geocoder.description_for_number(ch_number, "de")
'Zürich'
>>> geocoder.description_for_number(ch_number, "en")
'Zurich'
>>> geocoder.description_for_number(ch_number, "fr")
'Zurich'
>>> geocoder.description_for_number(ch_number, "it")
'Zurigo'
For mobile numbers in some countries, you can also find out information about which carrier originally owned a phone number.
from phonenumbers import carrier
ro_number = phonenumbers.parse("+40721234567", "RO")
carrier.name_for_number(ro_number, "en")
‘Vodafone’
For more Python related blogs Visit Us Geekycodes . Follow us on Instagram