python 小练习之生成*

需求分析:

1 将固定的号码段放到list中 如:136 137 180 183等等

2 随机取8个数字元素 

3 将固定号码段与随机产生的元素拼接在一起 

4 写入文件

import string
def phone_num(num):
   all_phone_nums=set()
   num_start = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '158', '159', '157', '182', '187', '188',
    '147', '130', '131', '132', '155', '156', '185', '186', '133', '153', '180', '189']
   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_num.txt','w',encoding='utf-8') as fw:
               fw.writelines(all_phone_nums)
phone_num(1000)

原文地址:https://www.cnblogs.com/dwtt/p/7798923.html