python随机生成手机号
2017-12-06 08:54
417 查看
依据
根据2017年10月份最新的手机号正则进行编码, 正则如下: (13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}
代码
# -*- coding: utf-8 -*- import random def create_phone(): # 第二位数字 second = [3, 4, 5, 7, 8][random.randint(0, 4)] # 第三位数字 third = { 3: random.randint(0, 9), 4: [5, 7, 9][random.randint(0, 2)], 5: [i for i in range(10) if i != 4][random.randint(0, 8)], 7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)], 8: random.randint(0, 9), }[second] # 最后八位数字 suffix = random.randint(9999999,100000000) 4000 # 拼接手机号 return "1{}{}{}".format(second, third, suffix) # 生成手机号 phone = create_phone() print(phone)
运行结果
13937342780 15835720604 14589505530 ...
验证 (使用正则验证)
# -*- coding: utf-8 -*- import random import re def create_phone(): # 第二位数字 second = [3, 4, 5, 7, 8][random.randint(0, 4)] # 第三位数字 third = { 3: random.randint(0, 9), 4: [5, 7, 9][random.randint(0, 2)], 5: [i for i in range(10) if i != 4][random.randint(0, 8)], 7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)], 8: random.randint(0, 9), }[second] # 最后八位数字 suffix = random.randint(9999999,100000000) # 拼接手机号 return "1{}{}{}".format(second, third, suffix) # 生成手机号 phone = create_phone() print(phone) # 正则 reg = re.compile("(13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}") print("Test passed!" if reg.match(phone) else "Test failed!")
验证结果
18662182464 Test passed! 15896505277 Test passed! 14952715286 Test passed! ...
相关文章推荐
- python-生成随机手机号
- 基础python的手机号随机生成
- Python随机生成手机号、数字的方法详解
- Python实现随机生成手机号及正则验证手机号的方法
- Python随机生成手机号
- 随机验证码生成和获取——基于python tkinter、pytesseract实现
- Python在指定文件夹生成随机文件
- Python随机生成信用卡卡号的实现方法
- Java 随机生成中文姓名,手机号,邮编,住址
- Python 随机生成中文验证码
- 学习python:练习3.随机生成200个序列号存入文件
- 随机生成正太分布样本原理及python实现
- Python 生成MySQL 管理员随机复杂秘钥
- python 实现红包随机生成算法的简单实例
- Python之随机生成数random模块
- Python 随机生成DAG(有向无环图)
- python生成随机验证码(中文验证码)示例
- python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
- Java 随机生成中文姓名,手机号,邮编,住址