python之随机生成手机号

Posted 花开半夏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之随机生成手机号相关的知识,希望对你有一定的参考价值。


import random
‘‘‘
手机号的规则如下
第一位:1;
第二位:3,4,5,7,8
第三位:
3:0-9
4:5,6,7
5:0,1,2,3,5,6,7,8,9
7:6,7,8
8:0-9
‘‘‘
def telphone():
  second = random.choice([3,4,5,7,8])#第二位值,从此列表随机生成

  third = {
  3:random.randint(0,9),
  4:random.choice([5,7]),
  5:random.choice([0,1,2,3,5,6,7,8,9]),
  7:random.choice([6,7,8]),
  8:random.randint(0,9)
  }[second]#根据second的值,来生成第3位的值

  behind = ‘‘#定义个空字符串
  for i in range(8):
  behind = behind + str(random.randint(0,9))#8位数字中的每一位从0-9中生成,8次循环后,字符串相加成为8位数
  phone_number = str(1) + str(second) + str(third) + behind#四组字符相加,生成手机号
  print(phone_number)
  return phone_number

for i in range(10):
telphone()#调用生成手机号函数

以上是关于python之随机生成手机号的主要内容,如果未能解决你的问题,请参考以下文章

python Python中随机生成手机号

python随机生成手机号

python_生成随机手机号

python随机生成有效手机号码

python接口自动化中,注册接口随机生成手机号码

Python生成随机五位数——模仿手机验证码