Python造电话号码数据小工具

1、电话任意段位构造 ,可任意构造N条符合正则校验的电话号码;
'''
中国电信号段:133,153, 180,181,189,173, 177,149
中国联通号段:130,131,132,155,156,185,186,145,176,185
中国移动号段:134,135,136,137,138,139,150,151,152,158,159,182,183,184,147,178
11位
第一位 :1
第二位:3,4,5,7,8
第三位:根据第二位来确定
    3 + 【0-9】
    4 + 【5,7,9】
    5 + 【0-9】 !4
    7 + 【0-9】! 4and9
    8 + 【0-9】
后8位: 随机生成8个数字
'''
import random
import csv

# creat_phone()
# 生成电话号
def creat_phone():
    # 第二位
    second = [3, 4, 5, 7, 8][random.randint(0, 4)]
    # second = [1, 0, 2][random.randint(0, 2)]

    # 第三位的值根据第二位来确定
    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]
    # 后8位随机抽取
    suffix = ''
    for x in range(8):
        suffix = suffix + str(random.randint(0, 9))
    # 拼接
    return "1{}{}{}".format(second, third, suffix)


# 调用
# print(creat_phone())
file_open = open('/Users/hejian/Desktop/mobile_.csv', 'a+', encoding='utf-8')
csv_write = csv.writer(file_open)
csv_write.writerow(['mobile'])

# 将文件的写入封装成函数
def opend_wirte(file_path):
    file_open_path = open(file_path, 'a+', encoding='utf-8')
    file_write = csv.writer(file_open_path)
    file_write.writerow(['mobile'])

num = input('请输入生成的数量')
for index in range(0, int(num)):
    csv_write.writerow([creat_phone()])
    print(creat_phone())

if __name__ == "__main__":
    creat_phone()





PS: 在上述代码中,其 段位 可以任意构造,如:10段,11段,12段

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值