在 Python 中格式化电话号码的最佳方法是什么?

发布于 2024-11-29 09:46:53 字数 194 浏览 0 评论 0原文

如果我只有 10 位或更多数字的字符串,如何将其格式化为电话号码?

一些琐碎的例子:

555-5555
555-555-5555
1-800-555-5555

我知道这些不是格式化它们的唯一方法,如果我自己这样做,我很可能会遗漏一些东西。是否有 python 库或格式化电话号码的标准方法?

If all I have is a string of 10 or more digits, how can I format this as a phone number?

Some trivial examples:

555-5555
555-555-5555
1-800-555-5555

I know those aren't the only ways to format them, and it's very likely I'll leave things out if I do it myself. Is there a python library or a standard way of formatting phone numbers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一页 2024-12-06 09:46:53

对于库:电话号码(pypi来源)

Google 通用库的 Python 版本,用于解析、格式化、存储和验证国际电话号码。

自述文件不够充分,但我发现代码有详细记录。

for library: phonenumbers (pypi, source)

Python version of Google's common library for parsing, formatting, storing and validating international phone numbers.

The readme is insufficient, but I found the code well documented.

蝶…霜飞 2024-12-06 09:46:53

看起来您的示例格式为除最后一个之外的三位数字组,您可以编写一个简单的函数,使用千位分隔符并添加最后一位数字:

>>> def phone_format(n):                                                                                                                                  
...     return format(int(n[:-1]), ",").replace(",", "-") + n[-1]                                                                                                           
... 
>>> phone_format("5555555")
'555-5555'
>>> phone_format("5555555")
'555-5555'
>>> phone_format("5555555555")
'555-555-5555'
>>> phone_format("18005555555")
'1-800-555-5555'

Seems like your examples formatted with three digits groups except last, you can write a simple function, uses thousand seperator and adds last digit:

>>> def phone_format(n):                                                                                                                                  
...     return format(int(n[:-1]), ",").replace(",", "-") + n[-1]                                                                                                           
... 
>>> phone_format("5555555")
'555-5555'
>>> phone_format("5555555")
'555-5555'
>>> phone_format("5555555555")
'555-555-5555'
>>> phone_format("18005555555")
'1-800-555-5555'
破晓 2024-12-06 09:46:53

这是改编自 utdemir 的解决方案此解决方案将与Python 2.6一起使用,因为“,”格式化程序是Python中的新功能2.7.

def phone_format(phone_number):
    clean_phone_number = re.sub('[^0-9]+', '', phone_number)
    formatted_phone_number = re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1-", "%d" % int(clean_phone_number[:-1])) + clean_phone_number[-1]
    return formatted_phone_number

Here's one adapted from utdemir's solution and this solution that will work with Python 2.6, as the "," formatter is new in Python 2.7.

def phone_format(phone_number):
    clean_phone_number = re.sub('[^0-9]+', '', phone_number)
    formatted_phone_number = re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1-", "%d" % int(clean_phone_number[:-1])) + clean_phone_number[-1]
    return formatted_phone_number
不离久伴 2024-12-06 09:46:53

更详细,一个依赖项,但保证大多数输入的一致输出,并且写起来很有趣:

import re

def format_tel(tel):
    tel = tel.removeprefix("+")
    tel = tel.removeprefix("1")     # remove leading +1 or 1
    tel = re.sub("[ ()-]", '', tel) # remove space, (), -

    assert(len(tel) == 10)
    tel = f"{tel[:3]}-{tel[3:6]}-{tel[6:]}"

    return tel

输出:

>>> format_tel("1-800-628-8737")
'800-628-8737'
>>> format_tel("800-628-8737")
'800-628-8737'
>>> format_tel("18006288737")
'800-628-8737'
>>> format_tel("1800-628-8737")
'800-628-8737'
>>> format_tel("(800) 628-8737")
'800-628-8737'
>>> format_tel("(800) 6288737")
'800-628-8737'
>>> format_tel("(800)6288737")
'800-628-8737'
>>> format_tel("8006288737")
'800-628-8737'

没有幻数; ...如果您不喜欢整个简洁性:

def format_tel(tel):
    AREA_BOUNDARY = 3           # 800.6288737
    SUBSCRIBER_SPLIT = 6        # 800628.8737
    
    tel = tel.removeprefix("+")
    tel = tel.removeprefix("1")     # remove leading +1, or 1
    tel = re.sub("[ ()-]", '', tel) # remove space, (), -

    assert(len(tel) == 10)
    tel = (f"{tel[:AREA_BOUNDARY]}-"
           f"{tel[AREA_BOUNDARY:SUBSCRIBER_SPLIT]}-{tel[SUBSCRIBER_SPLIT:]}")

    return tel

More verbose, one dependency, but guarantees consistent output for most inputs and was fun to write:

import re

def format_tel(tel):
    tel = tel.removeprefix("+")
    tel = tel.removeprefix("1")     # remove leading +1 or 1
    tel = re.sub("[ ()-]", '', tel) # remove space, (), -

    assert(len(tel) == 10)
    tel = f"{tel[:3]}-{tel[3:6]}-{tel[6:]}"

    return tel

Output:

>>> format_tel("1-800-628-8737")
'800-628-8737'
>>> format_tel("800-628-8737")
'800-628-8737'
>>> format_tel("18006288737")
'800-628-8737'
>>> format_tel("1800-628-8737")
'800-628-8737'
>>> format_tel("(800) 628-8737")
'800-628-8737'
>>> format_tel("(800) 6288737")
'800-628-8737'
>>> format_tel("(800)6288737")
'800-628-8737'
>>> format_tel("8006288737")
'800-628-8737'

Without magic numbers; ...if you're not into the whole brevity thing:

def format_tel(tel):
    AREA_BOUNDARY = 3           # 800.6288737
    SUBSCRIBER_SPLIT = 6        # 800628.8737
    
    tel = tel.removeprefix("+")
    tel = tel.removeprefix("1")     # remove leading +1, or 1
    tel = re.sub("[ ()-]", '', tel) # remove space, (), -

    assert(len(tel) == 10)
    tel = (f"{tel[:AREA_BOUNDARY]}-"
           f"{tel[AREA_BOUNDARY:SUBSCRIBER_SPLIT]}-{tel[SUBSCRIBER_SPLIT:]}")

    return tel
悲凉≈ 2024-12-06 09:46:53

您可以使用函数 clean_phone()< /a> 来自库 DataPrep。使用 pip install dataprep 安装它。

>>> from dataprep.clean import clean_phone
>>> df = pd.DataFrame({'phone': ['5555555', '5555555555', '18005555555']})
>>> clean_phone(df, 'phone')
Phone Number Cleaning Report:                                                   
    3 values cleaned (100.0%)
Result contains 3 (100.0%) values in the correct format and 0 null values (0.0%)
         phone     phone_clean
0      5555555        555-5555
1   5555555555    555-555-5555
2  18005555555  1-800-555-5555

You can use the function clean_phone() from the library DataPrep. Install it with pip install dataprep.

>>> from dataprep.clean import clean_phone
>>> df = pd.DataFrame({'phone': ['5555555', '5555555555', '18005555555']})
>>> clean_phone(df, 'phone')
Phone Number Cleaning Report:                                                   
    3 values cleaned (100.0%)
Result contains 3 (100.0%) values in the correct format and 0 null values (0.0%)
         phone     phone_clean
0      5555555        555-5555
1   5555555555    555-555-5555
2  18005555555  1-800-555-5555
原野 2024-12-06 09:46:53

一个简单的解决方案可能是从后面开始,在四个数字后面插入连字符,然后以三个为一组,直到到达字符串的开头。我不知道内置函数或类似的东西。

您可能会发现这很有帮助:
http://www.diveintopython3.net/regular-expressions.html#phonenumbers

如果您接受用户输入的电话号码,则正则表达式将很有用。我不会使用上面链接中遵循的确切方法。一些更简单的事情,比如去掉数字,可能更容易,而且同样好。

此外,在数字中插入逗号是一个类似的问题,该问题已在其他地方得到有效解决,并且可以适用于该问题。

A simple solution might be to start at the back and insert the hyphen after four numbers, then do groups of three until the beginning of the string is reached. I am not aware of a built in function or anything like that.

You might find this helpful:
http://www.diveintopython3.net/regular-expressions.html#phonenumbers

Regular expressions will be useful if you are accepting user input of phone numbers. I would not use the exact approach followed at the above link. Something simpler, like just stripping out digits, is probably easier and just as good.

Also, inserting commas into numbers is an analogous problem that has been solved efficiently elsewhere and could be adapted to this problem.

爱人如己 2024-12-06 09:46:53

就我而言,我需要按国家/地区获取类似“*** *** ***”的电话模式。

所以我在我们的项目中重新使用了 phonenumbers

from phonenumbers import country_code_for_region, format_number, PhoneMetadata, PhoneNumberFormat, parse as parse_phone
import re

def get_country_phone_pattern(country_code: str):
    mobile_number_example = PhoneMetadata.metadata_for_region(country_code).mobile.example_number
    formatted_phone = format_number(parse_phone(mobile_number_example, country_code), PhoneNumberFormat.INTERNATIONAL)
    without_country_code = " ".join(formatted_phone.split()[1:])
    return re.sub("\d", "*", without_country_code)

get_country_phone_pattern("KG")  # *** *** ***

In my case, I needed to get a phone pattern like "*** *** ***" by country.

So I re-used phonenumbers package in our project

from phonenumbers import country_code_for_region, format_number, PhoneMetadata, PhoneNumberFormat, parse as parse_phone
import re

def get_country_phone_pattern(country_code: str):
    mobile_number_example = PhoneMetadata.metadata_for_region(country_code).mobile.example_number
    formatted_phone = format_number(parse_phone(mobile_number_example, country_code), PhoneNumberFormat.INTERNATIONAL)
    without_country_code = " ".join(formatted_phone.split()[1:])
    return re.sub("\d", "*", without_country_code)

get_country_phone_pattern("KG")  # *** *** ***
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文