首页 Python

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import json
import time
import sys

def getAddressNum(Status = False):
    try:

        with open("addressNum.json","rb") as addressNumJson:
            address = json.load(addressNumJson)
        addressNums = list(address.values())
        addressNames = list(address.keys())
    except:
        sys.exit("文件不存在")
    if Status:
        addressTemp = input("请输入省/城市/城市/地址码:\n")
        if addressTemp.isnumeric() and len(addressTemp) == 6 and addressTemp in addressNums:
            print(addressNames[addressNums.index(addressTemp)])
            return addressTemp
        elif addressTemp in addressNames:
            return address[addressTemp]
        else:
            pass
    else:
        pass
    addressNum = random.sample(addressNums, 1)[0]
    print("随机生成:" + addressNames[addressNums.index(addressNum)])
    return addressNum

def getBirthdayNum():
    # 开始出生时间戳
    startBirthday = int(time.mktime((1971, 1, 1, 0, 0, 0, 0, 0, 0)))
    # 结束时间戳
    endBirthday = int(time.time())
    # 随机出生时间戳
    birthdayTime = random.randint(startBirthday,endBirthday)
    # 获得字符串时间
    birthdayNum = time.strftime("%Y%m%d",time.localtime(birthdayTime))
    return birthdayNum

def getOrderNum():
    orderNum = random.randint(0,999)
    if orderNum < 10:
        orderNum = "00" + str(orderNum)
    elif orderNum < 100:
        orderNum = "0" + str(orderNum)
    else:
        pass
    return str(orderNum)

def getWeightNum(top17):
    # 计算方法
    #
    # 1、身份证前17位分别乘以不同的系数(权重)。
    # 从第一位到第十七位的系数分别为:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2。
    #
    # 2、将这17位数字和系数相乘的结果相加。
    #
    # 3、相加的结果除以11,取余。
    #
    # 4、余数只可能有0-1-2-3-4-5-6-7-8-9-10
    # 分别对应的最后一位身份证的号码为1-0-X-9-8-7-6-5-4-3-2

    # 权重项
    weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    # 校验码映射值
    checkcode = {'0': '1', '1': '0', '2': 'X', '3': '9', '4': '8', '5': '7', '6': '6', '7': '5', '8': '4', '9': '3',
                 '10': '2'}
    count = 0
    for i in range(0,len(top17)):
        count = count + int(top17[i]) * weight[i]
    return top17 + checkcode[str(count%11)]

def run(resultNum, Status = False):
    AddresNum = getAddressNum(Status)
    for i in range(resultNum):
        print(getWeightNum(AddresNum + getBirthdayNum() + getOrderNum()))

if __name__ == '__main__':
    run(10, False)


文章评论