📜  Python中的电话号码模块

📅  最后修改于: 2022-05-13 01:54:49.326000             🧑  作者: Mango

Python中的电话号码模块

Python是一种非常强大的语言,并且库也非常丰富。 phonenumbers是提供许多功能的模块之一,例如提供电话号码的基本信息、电话号码的验证等。在这里,我们将通过编写简单的Python程序来学习如何使用 phonenumbers 模块。这是 Google 的 libphonenumber 库的Python端口。

安装

通过在命令提示符下键入以下命令来安装 phonenumbers 模块。

pip install phonenumbers

入门

1. 将String转换为phonenumber格式:为了探索phonenumbers模块的功能,我们需要将用户的电话号码转换为phonenumber格式。在这里,我们将看到如何将用户电话号码转换为电话号码格式。输入必须是字符串类型,并且必须在电话号码前添加国家代码。

Python3
# Program to convert input to
# phonenumber format
  
import phonenumbers
  
# Parsing String to Phone number
# Phone number format: (+Countrycode)xxxxxxxxxx
phoneNumber = phonenumbers.parse("+919876543210")
  
# This will print the phone number and 
# it's basic details.
print(phoneNumber)


Python3
# Program to get timezone a phone number
  
import phonenumbers
from phonenumbers import timezone
  
# Parsing String to Phone number
phoneNumber = phonenumbers.parse("+919876543210")
  
# Pass the parsed phone number in below function
timeZone = timezone.time_zones_for_number(phoneNumber)
  
# It print the timezone of a phonenumber
print(timeZone)


Python3
# Program to extract phone numbers from a text
import phonenumbers
  
# Text Input
text = "Contact us at +919876543210 or +14691234567"
  
# Pass the text and country code to the below function
numbers = phonenumbers.PhoneNumberMatcher(text, "IN")
  
# Printing the phone numbers matched with country code
# and also the indexes of the phone numbers in the string input
for number in numbers:
    print(number)


Python3
# Program to find carrier and region
# of a phone number
import phonenumbers
from phonenumbers import geocoder, carrier
  
# Parsing String to Phone number
phoneNumber = phonenumbers.parse("+919876543210")
  
# Getting carrier of a phone number
Carrier = carrier.name_for_number(phoneNumber, 'en')
  
# Getting region information
Region = geocoder.description_for_number(phoneNumber, 'en')
  
# Printing the carrier and region of a phone number
print(Carrier)
print(Region)


Python3
# Program to check whether a phone number is
# valid or not
import phonenumbers
  
# Parsing String to Phone number
phone_number = phonenumbers.parse("+91987654321")
  
# Validating a phone number
valid = phonenumbers.is_valid_number(phone_number)
  
# Checking possibility of a number
possible = phonenumbers.is_possible_number(phone_number)
  
# Printing the output
print(valid)
print(possible)


输出:

Country Code: 91 National Number: 9876543210

2. 获取时区:这是一个简单的Python程序,使用 phonenumbers 模块获取电话号码的时区。首先,我们将输入的字符串解析为 phonenumber 格式,然后使用内置函数获取用户的时区。它仅给出有效数字的输出。

Python3

# Program to get timezone a phone number
  
import phonenumbers
from phonenumbers import timezone
  
# Parsing String to Phone number
phoneNumber = phonenumbers.parse("+919876543210")
  
# Pass the parsed phone number in below function
timeZone = timezone.time_zones_for_number(phoneNumber)
  
# It print the timezone of a phonenumber
print(timeZone)

输出:

('Asia/Calcutta',)

3. 从文本中提取电话号码:我们可以使用此模块提取文本/段落中存在的电话号码。您可以对其进行迭代以检索一系列电话号码。为此, PhoneNumberMatcher对象提供了相关函数。

Python3

# Program to extract phone numbers from a text
import phonenumbers
  
# Text Input
text = "Contact us at +919876543210 or +14691234567"
  
# Pass the text and country code to the below function
numbers = phonenumbers.PhoneNumberMatcher(text, "IN")
  
# Printing the phone numbers matched with country code
# and also the indexes of the phone numbers in the string input
for number in numbers:
    print(number)

输出:

PhoneNumberMatch [14,27) +919876543210

4.电话号码的运营商和地区:这里我们将学习如何使用地理编码器找到电话号码的运营商和地区 和承运人 该模块的功能。

Python3

# Program to find carrier and region
# of a phone number
import phonenumbers
from phonenumbers import geocoder, carrier
  
# Parsing String to Phone number
phoneNumber = phonenumbers.parse("+919876543210")
  
# Getting carrier of a phone number
Carrier = carrier.name_for_number(phoneNumber, 'en')
  
# Getting region information
Region = geocoder.description_for_number(phoneNumber, 'en')
  
# Printing the carrier and region of a phone number
print(Carrier)
print(Region)

输出:

Airtel
India

5. 验证电话号码:一个简单的Python程序,检查给定电话号码是否有效(例如它在指定的交换机中),并检查给定电话号码是否可能(例如它有正确的位数)。

Python3

# Program to check whether a phone number is
# valid or not
import phonenumbers
  
# Parsing String to Phone number
phone_number = phonenumbers.parse("+91987654321")
  
# Validating a phone number
valid = phonenumbers.is_valid_number(phone_number)
  
# Checking possibility of a number
possible = phonenumbers.is_possible_number(phone_number)
  
# Printing the output
print(valid)
print(possible)

输出:

False
True