#需求分析:
#1、前三位确定,可以定义一个list,如lis = ['131', '132', '133', '134', '135','136','137','138','139','158', '159', '185', '186', '188']
#2、后面的八位随机取,通过random.sample(str,8)
用list添加手机号:
def phone_num(num):
import random,string
all_nums = []
for i in range(num):
lis = ['131', '132', '133', '134', '135', '158', '159', '185', '186', '188']
start = random.choice(lis)
end = ''.join(random.sample(string.digits, 8))
res = start + end + '
'
if res not in all_nums:
all_nums.append(res)
with open('telephone.txt', 'w', encoding='utf8') as fw:
fw.writelines(all_nums)
phone_num(10)
用集合添加手机号:
def phone_num(num):
import random,string
all_phone_nums = set()
num_start = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '158', '159', '157']
for i in range(num):
start = random.choice(num_start)
end = ''.join(random.sample(string.digits,8))
res = start + end + '
'
all_phone_nums.add(res)
with open('phone.txt','w',encoding = 'utf8') as fw:
fw.writelines(all_phone_nums)
phone_num(20)