需求細(xì)化:
1.身份證必須能夠通過(guò)身份證校驗(yàn)程序。
2.通過(guò)查詢(xún),發(fā)現(xiàn)身份證號(hào)碼是有國(guó)家標(biāo)準(zhǔn)的,標(biāo)準(zhǔn)號(hào)為 GB 11643-1999 可以從百度下載到這個(gè)文檔
下載:GB11643-1999sfz(jb51.net).rar
現(xiàn)行身份證號(hào)為18位,分別為6位地址碼,8位生日,3位順序碼,一位校驗(yàn)碼。具體例子可見(jiàn)下圖。
前六位也是國(guó)家標(biāo)準(zhǔn),GB2260-2007。吐槽一下,國(guó)標(biāo)竟然沒(méi)有一個(gè)網(wǎng)站供全面檢索和免費(fèi)下載。。。還好國(guó)家統(tǒng)計(jì)局有這些公開(kāi)數(shù)據(jù)。可以從統(tǒng)計(jì)數(shù)據(jù)-》統(tǒng)計(jì)標(biāo)準(zhǔn)-》行政區(qū)劃代碼頁(yè)面內(nèi)找到最新數(shù)據(jù):http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201401/t20140116_501070.html (這個(gè)網(wǎng)頁(yè)上的數(shù)據(jù)可能會(huì)舊)
出生年月日是8位
順序碼是3位,男生末尾為基數(shù),女生末尾為偶數(shù)。
最后一位是校驗(yàn)碼。校驗(yàn)算法其實(shí)后面有很多數(shù)學(xué)道理,這里給出最簡(jiǎn)單的公式:
前17位數(shù)字每一位有一個(gè)權(quán)重值
將第i位上的權(quán)重值記作Wi,Wi的值為 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
將身份證第i位的數(shù)字記作Ai
則使用下列公式算出一個(gè)數(shù)
S= Sum(Ai*Wi) mod 11 ------------- Sum(Ai*Wi) 取11的模。
這樣S的取值如下表:
對(duì)每一個(gè)S做一個(gè)映射 Y,這樣就有如下的表
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就是最終的校驗(yàn)碼。
原型實(shí)現(xiàn)過(guò)程:
1.獲取區(qū)域規(guī)劃碼的list,并讀入一個(gè)dictionary的list中。dictionary結(jié)構(gòu)如下:
{"state":河北省,"city":滄州市,"district":運(yùn)河區(qū),"code":130903}
丑陋的原型如下:
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})
上部你得到了一個(gè)codelist,里邊有所有的區(qū)號(hào)了。
下面是生成身份證號(hào)的原型,基本上是隨機(jī)生成
def gennerator(): id = codelist[random.randint(0,len(codelist))]['code'] #地區(qū)項(xiàng) id = id + str(random.randint(1930,2013)) #年份項(xiàng) da = date.today()+timedelta(days=random.randint(1,366)) #月份和日期項(xiàng) id = id + da.strftime('%m%d') id = id+ str(random.randint(100,300))#,順序號(hào)簡(jiǎn)單處理 i = 0 count = 0 weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] #權(quán)重項(xiàng) checkcode ={'0':'1','1':'0','2':'X','3':'9','4':'8','5':'7','6':'6','7':'5','8':'5','9':'3','10':'2'} #校驗(yàn)碼映射 for i in range(0,len(id)): count = count +int(id[i])*weight[i] id = id + checkcode[str(count%11)] #算出校驗(yàn)碼 return id
新聞熱點(diǎn)
疑難解答
圖片精選