Python-based ID card number Automatic Generation Program

Source: Internet
Author: User
Tags timedelta
I received a small demand today: a small program that automatically generates an ID card number. Recently, many python are used, so we plan to use python to refine the requirements:

1. The ID card must pass the ID card verification program.

2. You can find that the ID card number has a national standard. The standard number is GB 11643-1999, which can be downloaded from Baidu.

Download: gb11643-5osfz(bitscn.com#.rar

The current ID card number is 18 BITs, which are 6 bits, 8 bits, 3 bits, and 1 bits. The specific example can be seen.

The first six are also national standards, GB2260-2007. The Chinese national standard does not have a website for comprehensive search and free download... Fortunately, the National Bureau of Statistics has such public data. You can find the latest data on the statistic-statistic criteria-administrative division code page: http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html (the data on this page may be old)

The date of birth is 8 digits

The sequence code is three digits, with the base number at the end of the male and an even number at the end of the female.

The last digit is the verification code. There are many mathematical principles behind the validation algorithm. Here is the simplest formula:

Each of the first 17 digits has a weight value.

The weight value on the I-bit is recorded as Wi, And the Wi value is 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

Mark the I-digit of the ID card as Ai

Use the following formula to calculate a number.

S = Sum (Ai * Wi) mod 11 ------------- Sum (Ai * Wi) takes the model of 11.

The values of S are as follows:

Perform a ing Y for every second, so that the following table is available.

S: 0 1 2 3 4 5 6 7 8 9 10
Y: 1 0X9 8 7 6 5 4 3 2

Y is the final verification code.

Prototype implementation process:

1. Obtain the list of region planning codes and read them into a list of dictionary codes. The dictionary structure is as follows:

{"State": Hebei Province, "city": Cangzhou city, "district": Canal district, "code": 130903}

The ugly prototype is as follows:

def getdistrictcode():  with open('districtcode') as file:    data = file.read()  districtlist = data.split('\n')  global codelist  codelist = []  for node in districtlist:    #print node    if node[10:11] != ' ':      state = node[10:].strip()    if node[10:11]==' 'and node[12:13]!=' ':      city = node[12:].strip()    if node[10:11] == ' 'and node[12:13]==' ':      district = node[14:].strip()      code = node[0:6]      codelist.append({"state":state,"city":city,"district":district,"code":code})

In the upper part, you get a codelist with some area codes in it.

The following is a prototype for generating ID card numbers, which are basically randomly generated.

Def gennerator (): id = codelist [random. randint (0, len (codelist)] ['code'] # region item id = id + str (random. randint (1930,2013) # year item da = date. today () + timedelta (days = random. randint (1,366) # month and Date Item id = id + da. strftime ('% m % D') id = id + str (random. randint (100,300) #, sequence number simple processing I = 0 count = 0 weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # weight item checkcode = {'0': '1', '1': '0', '2 ': 'X', '3': '9', '4': '8', '5': '7', '6': '6', '7 ': '5', '8': '5', '9': '3', '10': '2'} # Check Code ing for I in range (0, len (id): count = count + int (id [I]) * weight [I] id = id + checkcode [str (count % 11)] # Calculate the checkcode return id

The prototype uses two standard python libraries.

From datetime import date
From datetime import timedelta

In this way, we can initially meet our needs and refine the tools as needed.

Btw, in fact, is developing a tool set for generating test data, and is striving for open source in the near future. If you have any specific requirements, please submit them to me. Those who want to work together are also very welcome :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.