received a small request today : A small program that automatically generates an ID number is required. Python is used more recently, so it is intended to be implemented in Python.
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 Http://zhidao.baidu.com/share/b838c7683bb70af8166efd810a398fec.html
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:
defGetdistrictcode (): With open ('Districtcode') as File:data=file.read () districtlist= Data.split ('\ n') Globalcodelist codelist= [] forNodeinchdistrictlist:#Print Node ifNODE[10:11]! =' ': State= node[10:].strip ()ifnode[10:11]==' ' andnode[12:13]!=' ': City= node[12:].strip ()ifNode[10:11] = =' ' andnode[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
defGennerator (): ID= Codelist[random.randint (0,len (codelist)) ['Code']#Area Itemid = id + str (random.randint (1930,2013))#Year Itemda = Date.today () +timedelta (Days=random.randint (1,366))#Month and date itemsid = id + da.strftime ('%m%d') ID= id+ Str (random.randint (100,300))#, Sequence number simple processingI=0 Count=0 Weight= [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]#Weight ItemsCheckcode ={'0':'1','1':'0','2':'X','3':'9','4':'8','5':'7','6':'6','7':'5','8':'5','9':'3','Ten':'2'}#Check Code mapping forIinchRange (0,len (ID)): Count= Count +int (id[i]) *Weight[i] ID= ID + checkcode[str (count%11)]#Calculate the check code returnId
The prototype was used in Python's two standard libraries.
from Import Date from 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:)