如何清洗并标准化印尼手机号码(以62开头的11位有效号码)
技术百科
聖光之護
发布时间:2026-01-24
浏览: 次 本文提供一个健壮的python函数,用于批量清洗手机号列表:自动移除符号与空格、补全国家代码62、验证长度,并将无效号码统一替换为"invalid number"。
在处理用户输入的手机号时,格式混乱(如含空格、短横线、+号、前导零或错误国家码)是常见问题。针对印尼手机号规范——必须以62开头,且去除62后剩余部分恰好为11位纯数字——我们需要一套确定性清洗逻辑,而非仅依赖正则匹配(如\+62[0-9]{11}仅能匹配已含+62的完整串,无法覆盖082...或82...等常见变体)。
以下是推荐实现方案,兼顾可读性、鲁棒性和完整性:
import re
def clean_phone_number(phone_list):
cleaned = []
for raw in phone_list:
# 步骤1:提取所有连续数字(移除 +, -, 空格等非数字字符)
digits = re.sub(r'\D', '', str(raw))
# 步骤2:标准化前缀
if digits.startswith('62'):
# 已有62 → 直接取全部数字
candidate = digits
elif digits.startswith('08'):
# 常见印尼本地号(08开头),补6
2 → 628...
candidate = '62' + digits[1:]
elif digits.startswith('8'):
# 无前导0的8开头号(如'82123...'),补62 → 6282123...
candidate = '62' + digits
else:
# 其他情况(如'14045')→ 无法合规补全,标记为无效
cleaned.append('Invalid Number')
continue
# 步骤3:验证总长度是否为13位(62 + 11位)
if len(candidate) == 13 and candidate.isdigit():
cleaned.append(int(candidate)) # 按示例输出转为整数
else:
cleaned.append('Invalid Number')
return cleaned
# 测试数据
phone = [
'82123321123',
'082321123321',
'+6282-456-654-456',
'+62 82 789 987 789',
'14045',
'82145-451-145'
]
result = clean_phone_number(phone)
print(result)
# 输出:[6282123321123, 6282321123321, 6282456654456, 6282789987789, 'Invalid Number', 6282145451145]✅ 关键设计说明:
- re.sub(r'\D', '', ...) 安全提取所有数字,避免手动遍历字符;
- 分三类前缀逻辑(62/08/8)覆盖印尼号码主流格式,比单一正则更实用;
- 显式检查 len(candidate) == 13 确保“62 + 11位”结构,杜绝 62123(过短)或 62123456789012(过长)等边缘错误;
- 返回 int 类型以匹配示例输出(若需保留字符串形式,可删去 int() 转换)。
⚠️ 注意事项:
- 该函数不验证号码段有效性(如运营商号段),仅做格式合规性清洗;
- 若输入含 None 或非字符串类型,str(raw) 可防止报错,但建议上游做基础类型校验;
- 如需支持国际多国号码,应扩展前缀映射表(如 {'62': 11, '86': 11, '1': 10}),而非硬编码逻辑。
此方案已在给定测试用例中100%通过,可直接集成至数据预处理流水线。
# 移除
# 常见问题
# python
# 已有
# 而非
# 如需
# 并将
# 可直接
# app
# 已在
# int
# 编码
# 字符串
# 报错
# git
# len
# 遍历
# 字符串类型
# elif
# python函数
# number
# 印尼
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- Python与GPU加速技术_CUDA与Numba
- 如何使用Golang实现错误包装与传递_Golan
- Windows笔记本无法进入睡眠模式怎么办?(电源
- Win11怎么设置虚拟键盘_打开Win11屏幕键盘
- PHP怎么接收URL中的锚点参数_获取#后面参数值
- 如何在Golang中实现基础配置管理功能_Gola
- 微信里的php文件怎么变mp4_微信接收php转m
- php中常量能用::访问吗_类常量与作用域操作符使
- php下载安装包太大怎么下载_分卷压缩下载方法【教
- c# 如何用c#实现一个支持优先级的任务队列
- Win10怎样卸载iTunes_Win10卸载iT
- Windows10电脑怎么设置虚拟光驱_Win10
- Win11怎样安装搜狗输入法_Win11安装搜狗输
- Win10怎样安装PPT模板_Win10安装PPT
- MySQL 中使用 IF 和 CASE 实现查询字
- 如何在 Go 中高效缓存与分发网络视频流
- 如何在 Go 中正确测试带 Cookie 的 HT
- 当网站SEO排名下降时,如何应对?
- Win10电脑C盘红了怎么清理_Windows10
- Win11怎么查看显卡温度 Win11任务管理器查
- 静态属性修改会影响所有实例吗_php作用域操作符下
- Win11怎么关闭资讯和兴趣_Windows11任
- Win10如何更改网络连接_Windows10以太
- Win11怎么设置快速访问主页_Windows11
- php转exe用什么工具打包快_高效打包软件推荐【
- 如何使用正则表达式批量替换重复的 *- 模式为固定
- Python配置文件操作教程_JSONINIYAM
- 一文详解网站被黑客入侵挂马解决办法
- Win10如何更改任务栏高度_Windows10解
- 如何提升Golang程序I/O性能_Golang
- Win11怎么关闭通知中心_Windows11系统
- 如何使用Golang实现Web表单数据绑定_自动映
- Windows10如何查看蓝屏日志_Win10使用
- 如何在 Python 测试中动态配置 @backo
- Win10如何更改电脑休眠时间_Windows10
- How to Properly Use NumPy
- mac怎么查看wifi密码_MAC查看已连接WiF
- Win10怎么查看内存时序参数_Win10CPU-
- Windows10系统怎么查看CPU核心数_Win
- Win11怎么设置开机问候语_自定义Win11锁屏
- Python路径拼接规范_跨平台处理说明【指导】
- Windows10系统怎么查看系统版本_Win10
- Windows10电脑怎么查看硬盘通电时间_Win
- 如何在 ACF 中正确更新嵌套多层 Group 字
- Python项目维护经验_长期演进说明【指导】
- c++如何利用doxygen生成开发文档_c++
- Windows10怎么查看硬件信息_Windows
- 短链接怎么自定义还原php_修改解码规则适配需求【
- Win10路由器怎么隐藏ssid Win10隐藏w
- VSC怎么在PHP中调试MySQL_数据库交互排查


QQ客服