在用户数据清洗中需要简单地判断手机号的有效性,于是从网上查找到运营商的号段,通过简单判断手机号的前三位是不是在号段内和剩余的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