Python–生成测试电话号码

  • Post author:
  • Post category:python


生成指定数目的电话号码作为测试数据

方法1

#生成电话号码

def creat_phone_num(n):

import random

fp=open((‘e:\phone_num_’+str(n)+’.txt’),‘w’,encoding=‘utf-8’)

result=’’

for i in range(n):

result+=(str(random.randint(13000000001,13999999999))+’\n’)

fp.write(result)

fp.close()

return None

creat_phone_num(500)

方法2

#写一个可以生成手机号的函数,保证不能重复

def creat_phone_num(num):

phone_start=[137,138,139,136,135,133,130,131,132,158,159,156,151,152,153,157,189,188,187,186,185,183,177]

import random

phone_num_list=[]

count=num

while count>0:

phone_end=[]

for i in range(8):

s=random.choice(‘0123456789’)

phone_end.append(s)

end_num=’’.join(phone_end)

phone_num=str(random.choice(phone_start))+end_num

print(phone_num)

if phone_num not in phone_num_list:

phone_num_list.append(phone_num)



版权声明:本文为fenglj520原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。