Automatic generation program of ID number based on Python _python

Source: Internet
Author: User
Tags datetime timedelta

Requirements Refinement:

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

2. Through the inquiry, found that the ID card number is National standards, the standard number of GB 11643-1999 can be downloaded from Baidu to this document

Download: Gb11643-1999sfz (jb51.net). rar

The current ID number is 18 digits, 6-bit address code, 8-digit birthday, 3-bit sequential code, and one parity code. A concrete example shows the figure below.

The top six is also the national standard, gb2260-2007. Spit slot, GB even do not have a website for comprehensive search and free download ... Fortunately, the NBS has these public figures. Can be found in the statistical data-"statistical standards-" Administrative Division code page to find the latest data: http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html (The data on this page may be old)

Date of birth is 8 digits

The order code is 3 digits, the male end is the base, and the female end is even.

The last one is the check code. There are many mathematical reasons behind the verification algorithm, here is the simplest formula:

First 17 digits each has a weight value

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

Number of ID card digits as AI

The following formula is used to calculate a number

s= sum (ai*wi) mod-------------sum (AI*WI) takes 11 of the modulo.

So the value of S is the following table:

Make a mapping Y for each s so that it's like the following 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 checkout code.

Prototype implementation process:

1. Get the list of regional planning codes and read them into a dictionary list. 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})

Upper you get a codelist with all the area codes inside.

Here is the prototype that generates the ID number and is 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[st R (count%11)] #算出校验码 return
  ID

The prototype uses Python's two standard libraries

From datetime import Date
From datetime import Timedelta

This will be able to initially meet the needs of the following can be based on the tool to refine.

BTW, in fact, is developing a test data generation tool set, the recent fight for open source. Any specific needs can be brought to me. Have wanted to do together the classmate also greatly welcome:)

Related Article

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.