import re
pnumber = re.sub("[^0-9]", "", '(510) 757-5030') #替换非数字字符
if len(pnumber) < 10: #小于10位数不是电话号码
pass
else:
if pnumber[0] == '1':
pnumber = pnumber[1:]#strip 1st char if it's a 1 #去掉首位是1的电话
if len(pnumber) == 10:
pnumber = "+1 %s-%s-%s" % (pnumber[:3],pnumber[3:6],pnumber[6:]) #统一格式
else:
pass
print(pnumber)
网友评论