zoukankan      html  css  js  c++  java
  • python随机生成手机号

    依据
    根据2017年10月份最新的手机号正则进行编码, 正则如下:
    (13d|14[579]|15[^4D]|17[^49D]|18d)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)

    # 拼接手机号
    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("(13d|14[579]|15[^4D]|17[^49D]|18d)d{8}")
    print("Test passed!" if reg.match(phone) else "Test failed!")

    验证结果
    18662182464
    Test passed!

    15896505277
    Test passed!

    14952715286
    Test passed!

    ...
    原文:https://blog.csdn.net/xiaobuding007/article/details/78726833

  • 相关阅读:
    列表常用的方法
    python 三级联动
    tkinter--gui工具:MD5
    python PublicMethod方法如下<LoggerBase有使用>
    python config/config.ini 配置文件如下
    python browser engine封装
    python 页面基类 Page封装 →driver层的封装(最底层的封装Page)
    Eclipse配置maven环境
    appium 多个测试用例<多个 **.py文件>,只执行一次app <Python 3.8.1>
    python3版本使用weditor报错,解决方案
  • 原文地址:https://www.cnblogs.com/jiaoxiaohui/p/10702684.html
Copyright © 2011-2022 走看看