在Python中的多胞體

王林
發布: 2023-09-05 14:21:06
轉載
1029 人瀏覽過

在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, themodule may be set up. After installation, 文章 installer, themodule may be set up。 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 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 is potentially valid, meaning it has a valid country code is potentially valid, meaning it has a valid country code is potentially valid, meaning it valari a valid country code but may not valve for may sumbers val,

##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專案中準確且一致地處理電話號碼,增強功能、使用者體驗和資料完整性。

以上是在Python中的多胞體的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!