Hello everyone I am Zhengyin I'll teach you how to use it today python Code for ID number verification implementation example
Let's talk about the verification rules for ID number
In other words, ID number verification , The most important thing is to verify . So how to verify ? How to have 15、18 It can be divided into two ID number ?
1、1999 year 07 month 01 Use... Days ago 15 ID card No , That is, the first generation ID card
2、 The number of digits of the second-generation ID card has been increased to 18, Extra 3 Bits are the first two digits of the year of birth and a check code
3、 The representative information of each position of the ID card is as follows
4、 The first two digits of the sequence code represent the code of the local police station , The third place is the same year 、 In the same month 、 The sequence number of people born on the same day , The odd number of sequence codes is assigned to men , Even numbers are allocated to women .
5、 Check code , ID number 18 position , front 17 Bit is the body code , The last digit is the calculated check code . The verification rules are as follows :
front 17 The bits are multiplied by 【7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2】
And then 17 A product plus a piece is the remainder 11 Get one 0-10 Number of ranges
0-10 They correspond to each other 【1,0,x,9,8,7,6,5,4,3,2】 That is, check digit number
appear x Because the check code is 10 But in order to keep the length of the number unchanged , Instead of x
python The verification ID number code is as follows :
version1.1
def check_id_length(n):
if len(str(n)) != 18:
print(" Only support 18 Bit ID number query ")
return False
else:
return True
def check_id_data(n):
var=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
var_id=['1','0','x','9','8','7','6','5','4','3','2']
n = str(n)
sum = 0
if int(n[16])%2==0:
gender=" Woman "
same=int(int(n[16])/2)
else:
gender=" male "
same=int((int(n[16])+1)/2)
for i in range(0,17):
sum += int(n[i])*var[i]
sum %= 11
if (var_id[sum])==str(n[17]):
print(" The ID number has passed the verification , The check code is :",var_id[sum])
print(" Born in :",n[6:10]," year ",n[10:12]," month ",n[12:14]," Japan "," Gender :",gender,"\n Local same sex birthday ranking :",same)
return sum
else:
print(" Born in :",n[6:10]," year ",n[10:12]," month ",n[12:14]," Japan "," Gender :",gender,"\n Local same sex birthday ranking :",same)
print(" But the verification of the ID number rule failed , The check code should be ",var_id[sum],", The current check code is :",n[17])
return 0
n = input(" Please enter 18 ID card No :")
if check_id_length(n):
check_id_data(n)
else:
print(" Please re-enter ")
Code instructions
1、 The first six lines define the length check function , Check whether the length is 18
2、7-28 Line defines the check bit verification function , Verify that the check bit is correct
3、29-33 Line calls the above two line numbers to complete the verification function
among
1、 The first 8 OK, it defines List of weighted numbers , The list element type is int
2、 The first 9 OK, it defines List of standard check bits , The list element type is str
3、 The first 10 Will receive ID number converted to str type , In order to facilitate the slice to raise the middle position
4、12-17 Rows are used to calculate ID number primary gender And local ( The jurisdiction of the police station ) The order of people of the same sex and the same birthday
5、18-20 Weighted summation and remainder are performed respectively , This is also the core algorithm of this small code
6、21-28 Check bit comparison , And output the comparison result , At the same time, the date of birth
Demo code , Add display judgment
verion1.2
import time
def check_id_length(n):
if len(str(n)) != 18:
print(" Only support 18 Bit ID number query ")
return False
else:
return True
def check_id_data(n):
n = str(n)
n2 = str(n[:16])
time_now = int(time.strftime("%Y",time.localtime()))
is_digit = (not(n2.isdigit())) or (not(n[17].isdigit()) and (n[17]) !="x")
if (is_digit):
print(" I'm sorry , Here is your Mars ID card , Temporarily not accepted ")
elif (int(n[6:10]))>time_now:
print(n[6:10]," Man of the year ? Are you coming back through ?")
elif (int(n[10:12]))>12:
print(n[10:12]," born ?, You picked it up !!")
elif (int(n[12:14]))>31:
print(n[12:14]," Day born ?, You must have paid for it ")
else:
check_id_data2(n)
def check_id_data2(n):
var=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
var_id=['1','0','x','9','8','7','6','5','4','3','2']
sum = 0
if int(n[16])%2==0:
gender=" Woman "
same=int(int(n[16])/2)
else:
gender=" male "
same=int((int(n[16])+1)/2)
for i in range(0,17):
sum += int(n[i])*var[i]
sum %= 11
if (var_id[sum])==str(n[17]):
print(" The ID number has passed the verification , The check code is :",var_id[sum])
print(" Born in :",n[6:10]," year ",n[10:12]," month ",n[12:14]," Japan "," Gender :",gender,"\n Local same sex birthday ranking :",same)
return sum
else:
print(" Born in :",n[6:10]," year ",n[10:12]," month ",n[12:14]," Japan "," Gender :",gender,"\n Local same sex birthday ranking :",same)
print(" But the verification of the ID number rule failed , The check code should be ",var_id[sum],", The current check code is :",n[17])
return 0
while(1):
print("\n menu , Please enter a number \n----------------------")
print(" Input 1 Manually enter the ID number ")
print(" Input 2 Select test number ")
print(" Otherwise, goodbye ")
print("----------------------")
select = input("\n Please enter :")
if (select.isdigit()):
pass
else:
print(" It's agreed to input numbers , Goodbye to you ")
break
select = int(select)
if (select == 1):
n = input(" Please enter 18 ID card No :")
if check_id_length(n):
check_id_data(n)
else:
print(" Please re-enter ")
elif select == 2:
print("\n----------------------")
print(" Start verifying ID number :61011519920317602")
check_id_length(61011519920317602)
print("\n----------------------")
print(" Start verifying ID number :610115199203176021")
check_id_data(610115199203176021)
print("\n----------------------")
print(" Start verifying ID number :610115199203176028")
check_id_data(610115199203176028)
else:
break
The code test results are as follows :
Test chart II :
Test figure 3 :
For more tests, please run the code yourself !
There's a lot of room for optimization .
1、 Like joining 15 Verification of bit ID number , Uh huh ,15 Bit does not seem to be verified , Then you can only extract a date of birth or something .
2、 For example, judge whether to join the address database , Add output information
3、 Increase judgment time , If the date of birth is greater than the current time, it will be illegal
It's not easy to make Click a free attention to support bloggers
I am Zhengyin Looking forward to your attention