[Python actual combat] Python calculates ID verification code and generates virtual ID card in batch

There is a previous article that details some information about ID card. Please check the article. [test] past life and present life of ID card . Citizenship number is a feature combination code, which consists of 17 digit ontology code and one check code. The sequence from left to right is: six digit address code, eight digit birth date code, three digit sequence code and one digit verification code. In this paper, we calculate the last check code to generate virtual ID card in batch.

# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#By cacho_
#Blog: https://blog.csdn.net/sinat_37967865
#File: getIdCard.py
#Date: March 11, 2019
#Note: generate virtual ID card - > ID card composition: 6 digits of birth place + 8 digits of birth date + 2 digits of sequence code + 1 digit of gender + 1 digit of verification code 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import pymysql
import time

from logger import Logger

logger = Logger("getIdCard").get_log()

db = pymysql.connect(
            host="10.16.20.182",
            user="dev",
            password="dev",
            port=3306,
            use_unicode=True,
            charset="utf8",
            database="qdzjmsloandb")

cursor = db.cursor()

checkCode = {
    0:'1',
    1:'0',
    2:'X',
    3:'9',
    4:'8',
    5:'7',
    6:'6',
    7:'5',
    8:'4',
    9:'3',
    10:'2'
}

regions_dict = {
    "650102":"Tianshan District, Urumqi City, Xinjiang Uygur Autonomous Region",
    "650104":"Xinshi District, Urumqi, Xinjiang Uygur Autonomous Region",
    "650109":"Midong District, Urumqi City, Xinjiang Uygur Autonomous Region",
    "650106":"Toutunhe District, Urumqi, Xinjiang Uygur Autonomous Region"
}


def createTable():
    sql1 = 'drop table if exists zeng_idCardInfo ;'
    sql2 = 'create table zeng_idCardInfo (No int(11) AUTO_INCREMENT,id_region VARCHAR(20),id_regionInfo VARCHAR(20),id_birthday VARCHAR(20),id_card VARCHAR(20),PRIMARY KEY (No));'

    cursor.execute(sql1)
    cursor.execute(sql2)
    db.commit()


def get_idCard():
    # Total number = regions * birthdays * 800 4 * 10 * 800 = 32000 + 4 * 800
    idCards = []
    regions = ['650102','650104','650109','650106']
    #birthdays = ['19881103','19881104','19881105','19881106','19881107','19881108','19881109','19881110','19881111','19881112']
    birthdays = ['19901231']
    for region in regions:
        for birthday in birthdays:
            for i in range(100,900):
                # print(i,region,birthday)
                idCard = region+birthday+str(i)
                S = int(idCard[0:1]) * 7 + int(idCard[1:2]) * 9 + int(idCard[2:3]) * 10 + int(idCard[3:4]) * 5 + int(
                    idCard[4:5]) * 8 + int(idCard[5:6]) * 4 + int(idCard[6:7]) * 2 + int(idCard[7:8]) * 1 \
                    + int(idCard[8:9]) * 6 + int(idCard[9:10]) * 3 + int(idCard[10:11]) * 7 \
                    + int(idCard[11:12]) * 9 + int(idCard[12:13]) * 10 + int(idCard[13:14]) * 5 + int(
                    idCard[14:15]) * 8 + int(idCard[15:16]) * 4 + int(idCard[16:17]) * 2
                Y = S % 11
                last_code = checkCode[Y]
                # print(S,Y,last_code)
                id_card = idCard + last_code
                idCards.append(id_card)
                idCard_to_mysql(region,regions_dict[region],birthday,id_card)
    print("The number of ID cards generated is:", len(idCards))
    #return(idCards)


def get_oneCard(birthday):
    idCard = '110101' + birthday + '100'
    S = int(idCard[0:1]) * 7 + int(idCard[1:2]) * 9 + int(idCard[2:3]) * 10 + int(idCard[3:4]) * 5 + int(
        idCard[4:5]) * 8 + int(idCard[5:6]) * 4 + int(idCard[6:7]) * 2 + int(idCard[7:8]) * 1 \
        + int(idCard[8:9]) * 6 + int(idCard[9:10]) * 3 + int(idCard[10:11]) * 7 \
        + int(idCard[11:12]) * 9 + int(idCard[12:13]) * 10 + int(idCard[13:14]) * 5 + int(
        idCard[14:15]) * 8 + int(idCard[15:16]) * 4 + int(idCard[16:17]) * 2
    Y = S % 11
    last_code = checkCode[Y]
    # print(S,Y,last_code)
    id_card = idCard + last_code
    logger.info('The ID card information is:%s', id_card)



def idCard_to_mysql(id_region,id_regionInfo,id_birthday,id_card):
    sql = "insert into zeng_idCardInfo (id_region,id_regionInfo,id_birthday,id_card) values ('%s','%s','%s','%s');"\
          % (id_region,id_regionInfo,id_birthday,id_card)
    try:
        cursor.execute(sql)
    except Exception as e:
        # Rollback on error
        db.rollback()
        print(str(e))
    else:
        db.commit()  # Transaction submission
        logger.info('%s ID card information inserted successfully!', id_card)


if __name__ == '__main__':
    cTime = time.time()
    #createTable()
    #get_idCard()
    get_oneCard('19710516')
    logger.info("Total time:%s ", time.time() - cTime)

Through the above code, we find that three parameters are very critical:

regions area code, which can be customized
birthdays, which can be customized
Sequence code 3 digits, gender can be determined
Check code corresponding to the value calculated by checkCode

Keywords: SQL Database

Added by xsaero00 on Thu, 31 Oct 2019 13:59:17 +0200