卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章17596本站已运行3327

在Python中的多胞体

在Python中的多胞体

Introduction

的中文翻译为:

介绍

使用Python的phonenumbers包可以更轻松地解析、格式化和验证电话号码。该模块基于Google的libphonenumber包,为开发人员提供了一套强大的工具,以标准化的方式处理电话号码。phonenumbers模块可以从用户输入中提取电话号码,验证其准确性,并按照国际标准进行格式化。

在本文中,我们将探讨phonenumbers模块提供的众多功能和能力,并展示如何在实际应用中使用它们。我们将探索该模块的功能,并解释它如何使您的Python应用程序中的电话号码处理更简单,包括解析和验证、格式化和提取关键信息。

Phonenumbers模块在Python中的实现

安装和引入

The phonenumbers module has to be installed and loaded into our Python environment before we can explore its features. Using pip, Python's package installer, the module may be set up. After installation, we can use the import statement to add the phonenumbers module to our interactive Python sessions or scripts.

Example

'
pip install phonenumbers import 
phonenumbers 

输出

'
Looking 	in 	indexes: 	https://pypi.org/simple, https://us-python.pkg.dev/colabwheels/public/simple/ 
Collecting phonenumbers 
  Downloading phonenumbers-8.13.11-py2.py3-none-any.whl (2.6 MB) 
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.6/2.6 
MB 37.6 MB/s eta 0:00:00 
Installing collected packages: phonenumbers 
Successfully installed phonenumbers-8.13.11 

Parsing Phone Numbers

phonenumbers模块的主要任务之一是从各种字符串表示中解析电话号码。无论电话号码是否带有国家代码,是否带有破折号或空格,该模块提供的parse()函数都可以智能地解释和解析电话号码。解析后的电话号码对象包含有用的信息,如国家代码、国内号码、扩展等。

Example

的中文翻译为:

示例

'
import phonenumbers 
 
# Parse a phone number 
number = "+14155552671" 
parsed_number = phonenumbers.parse(number, None) 
 
# Accessing  parsed information 
print(parsed_number.country_code)  #    Output:         1 
print(parsed_number.national_number)  # Output:4155552671 
print(parsed_number.extension)  # Output: None 

输出

'
1 
4155552671 
None 

电话号码验证

Validating phone numbers is crucial to ensure that they adhere to the correct format and are potentially reachable. The phonenumbers module provides various validation methods to check if a phone number is valid or possible. The is_valid_number() function determines if the phone number is valid based on its syntax and structure. On the other hand, the is_possible_number() function checks if the phone number is potentially valid, meaning it has a valid country code but may not necessarily conform to all the rules for a valid number.

Example

的中文翻译为:

示例

'
import phonenumbers 
 
# Validate phone numbers 
valid_number = "+14155552671" 
invalid_number = "+1234567890" 
 
print(phonenumbers.is_valid_number(phonenumbers.parse(valid_number)))  # Output: True 
print(phonenumbers.is_valid_number(phonenumbers.parse(invalid_number))) # Output: False 
 
print(phonenumbers.is_possible_number(phonenumbers.parse(valid_number)))  # Output: True 
print(phonenumbers.is_possible_number(phonenumbers.parse(invalid_number))) # 
Output: True 

输出

'
True 
False 
True 
False 

格式化电话号码

在向人们展示电话号码或将其保存在数据库之前,一致而统一地格式化电话号码非常重要。为了确保电话号码以适当的方式显示,phonenumbers模块提供了多种格式化选项。

您可以使用format_number()方法按照多种样式格式化解析后的电话号码,包括国际格式、国内格式和E.164格式。国内格式仅提供国内重要号码,不包括国家代码,而国际格式还包括国家代码。E.164格式包括国家代码和国内重要号码,这是标准化的格式,不使用任何格式化符号。

Example

的中文翻译为:

示例

'
import phonenumbers 
 
# Format phone numbers 
parsed_number =phonenumbers.parse("+14155552671") 
 
# Format as international formatprint(phonenumbers.format_number(parsed_number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)) 
 
# Format as national format 
print(phonenumbers.format_number(parsed_number, phonenumbers.PhoneNumberFormat.NATIONAL)) 
 
# Format as E.164 format 
print(phonenumbers.format_number(parsed_number, phonenumbers.PhoneNumberFormat.E164)) 

输出

'
+1 415-555-2671 
(415) 555-2671 
+14155552671 

从电话号码中提取信息

您可以使用phonenumbers模块从电话号码中收集重要数据。例如,您可以使用geocoder模块,一个phonenumbers子模块,来确定电话号码的位置。这种功能在应用程序中非常有帮助,当您需要知道电话号码属于哪个国家、地区或运营商时。

Example

的中文翻译为:

示例

'
import phonenumbers 
from phonenumbers import geocoder 
 
# Parse a phone number number = "+14155552671" 
parsed_number = phonenumbers.parse(number, None) 
 
# Retrieve the geographic information location = geocoder.description_for_number(parsed_number, "en")
 print(location)  # Output: United States 

输出

'
San Francisco, CA 

Advanced Usage: Geocoding and Carrier Lookup

借助phonenumbers模块的先进功能,如运营商查询和地理编码,您可以了解更多关于电话号码的信息。地理编码子模块提供了定位电话号码所属国家、地区和运营商的工具。

可以使用运营商子模块的功能来查找电话号码的运营商或服务提供商。需要特定于运营商的功能或需要验证运营商数据的应用程序可能会发现这很方便。

Example

的中文翻译为:

示例

'
import phonenumbers 
from phonenumbers import geocoder, carrier 
 
# Parse a phone number 
number = "+14155552671" 
parsed_number = phonenumbers.parse(number, None) 
 
# Retrieve the carrier information 
carrier_name = carrier.name_for_number(parsed_number, "en") 
print(carrier_name)  # Output: AT&T Mobility 
 
# Retrieve the geographic information 
location = geocoder.description_for_number(parsed_number, "en")
 print(location)  # Output: United States 

输出

'
‘***’ # cannot be disclosed 
San Francisco, CA 

处理国际电话号码

phonenumbers模块还提供处理国际电话号码的功能。它允许您确定电话号码的地区或国家,并根据该地区的约定进行格式化。这在处理来自不同国家的电话号码时特别有用。

Example

的中文翻译为:

示例

'
import phonenumbers 
 
# Parse a phone number 
number = "+353876543210"
parsed_number = phonenumbers.parse(number, None) 
 
# Get the region information region = phonenumbers.region_code_for_number(parsed_number) 
print(region)  # 

输出

'
IE

定制化和本地化

Python的phonenumbers模块提供了自定义的可能性,以满足独特的需求。通过创建新的信息或导入特定位置的元数据,您可以改变模块的行为方式。这使您能够管理常规元数据可能无法处理的电话号码。通过提供额外的信息,您可以扩展模块的功能,以支持特殊的电话号码格式或编号计划。

此外,phonenumbers模块支持本地化,使您能够以不同的语言和格式显示电话号码。您可以在格式化电话号码时指定所需的语言,确保输出符合指定语言的约定。这种本地化功能在全球用户基础的应用程序中特别有用,因为它通过以每个地区熟悉和适当的格式呈现电话号码来增强用户体验。

最佳实践和注意事项

处理敏感用户数据需要考虑隐私和安全问题。遵循适用的数据保护规定,并采取适当措施保护电话号码。采取必要的保护措施,防止电话号码信息被滥用或未经授权访问。

结论

Python中的phonenumbers模块是一个强大而方便的工具,用于解析、验证、格式化和管理电话号码。它具有广泛的功能,包括解析不同格式的电话号码、验证其正确性以及执行地理编码和运营商查找等高级操作,使其成为处理电话号码的应用程序的宝贵资产。通过支持国际电话号码、定制选项和本地化功能,phonenumbers模块为电话号码管理提供了全面的解决方案。通过利用这个模块,开发人员可以确保在他们的Python项目中准确和一致地处理电话号码,增强功能、用户体验和数据完整性。

卓越飞翔博客
上一篇: 如何使用 PHP 实现数据加工和数据清洗功能
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏