What#39;s the best way to format a phone number in Python?(在 Python 中格式化电话号码的最佳方法是什么?)
问题描述
如果我只有一个 10 位或更多位的字符串,我该如何将其格式化为电话号码?
If all I have is a string of 10 or more digits, how can I format this as a phone number?
一些简单的例子:
555-5555
555-555-5555
1-800-555-5555
我知道这些不是格式化它们的唯一方法,如果我自己做的话,我很可能会遗漏一些东西.是否有 python 库或格式化电话号码的标准方法?
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?
推荐答案
for library: phonenumbers (pypi,来源)
for library: phonenumbers (pypi, source)
用于解析、格式化、存储和验证国际电话号码的 Google 通用库的 Python 版本.
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.
这篇关于在 Python 中格式化电话号码的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中格式化电话号码的最佳方法是什么?


基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01