python笔记 之 手机号有效性简单判断

需求

在用户数据清洗中需要简单地判断手机号的有效性,于是从网上查找到运营商的号段,通过简单判断手机号的前三位是不是在号段内和剩余的8位是不是全为数字来简单判断用户手机是否有效。

号段

移动

移动号段包含139,138,137,136,135,134,147,150,151,152,157,158,159,178,182,183,184,187,188

联通

联通号段包含130,131,132,155,156,185,186,145,176

电信

电信号段包含133,153,177,173,180,181,189

虚拟号段

虚拟号段包含170,171

代码

class Verificate():
    def __init__(self):
        # 移动:
        self.hd_yd=[139,138,137,136,135,134,147,150,151,152,157,158,159,178,182,183,184,187,188]
        # 联通:
        self.hd_lt=[130,131,132,155,156,185,186,145,176 ]
        # 电信:
        self.hd_dx=[133,153,177,173,180,181,189]
        # 虚拟运营商:
        self.hd_xn=[170,171]
        self.hd_name=[self.hd_yd,self.hd_lt,self.hd_dx,self.hd_xn]
        self.hd_all=[]
        self.mobile3All()

    def mobile3All(self):
        for yys in self.hd_name:
            self.hd_all.extend(yys)

    def verificate(self,mobile:str):
        if len(mobile)!=11:
            return None
        try:
            hd,wh=int(mobile[:3]),int(mobile[3:])
        except Exception as err:
            print(mobile,str(err))
        else:
            if hd not in self.hd_all:
                return None
            else:
                return mobile

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值