Python-based ID number auto-generation program

Source: Internet
Author: User
Tags timedelta
Requirements Refinement:

1. The identity card must be able to pass the ID verification procedure.

2. Through the inquiry, found that the identity card number is a national standard, GB 11643-1999 can be downloaded from Baidu to this document

Download: Gb11643-1999sfz (bitscn.com). rar

The current ID number is 18 digits, 6 address code, 8 birthday, 3 bit sequence code, and one check code. Concrete examples are visible.

The top six are also national standards, gb2260-2007. Spit Groove, the national standard unexpectedly does not have a website for comprehensive search and free download ... Fortunately, the NBS has these public figures. You can find the latest data from the statistics-"statistical standards-" Administrative code page: http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html (The data on this page may be old)

Birth date is 8 people

The sequence code is 3 bits, the male end is the base, the female end is even.

The last one is the check code. There are many mathematical truths behind the calibration algorithm, and the simplest formula is given here:

The first 17 digits have a weight value for each bit

The value of the weight on position i is recorded as Wi,wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

The number I digits of the ID card as AI

The following formula is used to calculate a number

The s= sum (ai*wi) mod-------------sum (ai*wi) takes 11 modulo.

So the value of S is as follows table:

Make a map Y for each s, so it's like a table

s:0 1 2 3 4 5 6 7 8 9 10
Y:1 0 X 9 8 7 6 5 4 3 2

Y is the final check code.

Prototype implementation process:

1. Get a list of the area plan codes and read them into a list of dictionary. The dictionary structure is as follows:

{"state": Hebei Province, "City": Cangzhou, "District": Yunhe District, "code": 130903}

The ugly prototypes are 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]! = ":      St ate = 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":d istrict, "code": Code})

At the top you get a codelist with all the area codes inside.

Here is the prototype that generates the ID number, basically randomly generated

Def gennerator ():  id = codelist[random.randint (0,len (codelist)) [' Code '] #地区项  id = id + str (random.randint ( 1930,2013)) #年份项  da = Date.today () +timedelta (Days=random.randint (1,366)) #月份和日期项  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, 5, 8, 4, 2] #权重项  checkcode ={' 0 ': ' 1 ', ' 1 ': ' 0 ', ' 2 ': ' X ', ' 3 ': ' 9 ', ' 4 ': ' 8 ', ' 5 ': ' 7 ', ' 6 ': ' 6 ', ' 7 ': ' 5 ', ' 8 ': ' 5 ', ' 9 ': ' 3 ', ' Ten ': ' 2 ' #校验码映射 for  I in range (0,len (ID)):    count = Count +int (Id[i]) *weight[i]  id = id + checkcode[str (COUNT%11)] #算出校验码  return ID

The prototype was used in Python's two standard libraries.

From datetime import Date
From datetime import Timedelta

This will be able to meet the needs of the initial, follow-up can be based on the tools to refine.

BTW, in fact, is developing a tool set of test data generation that has recently sought open source. Any specific needs can be brought to me. Have wanted to do together classmates also greatly 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.