Verification of ID card information of Python actual combat small project
- 2021-12-11 07:55:30
- OfStack
python small project-ID card information verification
Purpose
Make a program, input ID number, output the following information
Gender: Male
Area: Yongding County, Longyan City, Fujian Province
Date of birth: May 1, 2000
The first boy registered at the police station on the day of birth
Check code: 0
Authenticity of ID number: true
Required documents and knowledge
ID card area code.xls (uploaded) Composition of ID card number ID card check code algorithmCheck code algorithm
18-digit ID card = 17-digit information data +1-digit check code1到6位 | 7到14位 | 15到16位 | 17位 | 18位 |
---|---|---|---|---|
区域代码 | 出生年月日 | 登记所代码 | 性别 | 校验码 |
1 means the first registered boy
3 means the second registered boy
And so on
Detailed Explanation of Verification Algorithm
1 to 17 bits multiplied by the corresponding weight and summed Take the remainder of the result over 11 Find the corresponding check code according to the remainderWeights for every 1 bit (17 bits of information data, from left to right, the first bit corresponds to 7, the second bit corresponds to 9, and so on)
weights=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
Dictionary of remainder and check code correspondence (2 corresponds to Roman numeral "X"-10)
verification_dict={0:1,
1:0,
2:"X",
3:9,
4:8,
5:7,
6:6,
7:5,
8:4,
9:3,
10:2}
Complete code presentation (you can do it yourself first)
import pandas as pd
import numpy as np
data = pd.read_excel(' ID card area code full version .xls')
# Read 18 ID card
sfz=input()
# Slicing ID card information
position=int(sfz[0:6])
birthday=sfz[6:14]
police_code=sfz[14:16]
sex_code=int(sfz[16])
verification=int(sfz[17])
# Get the name of the area, such as Yongding County, Longyan City, Fujian Province
position_name=data.iloc[data.loc[data[' Code '] == position].index[0]][' Name ']
# Determine the gender and determine which one is registered by the registration office
def sex(n):
train_list=[]
male_list=[1,3,5,7,9]
female_list=[0,2,4,6,8]
if n in male_list:
train_list.append(' Male ')
train_list.append(male_list.index(n)+1)
else:
train_list.append(' Female ')
train_list.append(female_list.index(n)+1)
return train_list
# Check algorithm
weights=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
sfz_code=[]
for item in sfz:
sfz_code.append(int(item))
weights_sfz=[]
for i in sfz_code:
for j in weights:
weights_sfz.append(i*j)
# Find the remainder
target=sum(weights_sfz)%11
verification_dict={0:1,
1:0,
2:"X",
3:9,
4:8,
5:7,
6:6,
7:5,
8:4,
9:3,
10:2}
# Find the number corresponding to the remainder 18 Bit check code
def verification_whether(target_n):
return verification_dict[target_n]
# See if the input ID card check code and the calculated check code are 1 To, if not 1 To, it is a fake ID card
def verification_true_false(sfz_verification,target_n_whether):
if sfz_verification == target_n_whether:
return " True "
else:
return " Fake "
# Format and print the results
print(" Gender : ",sex(sex_code)[0])
print(" Region : ",position_name)
print(" Date of birth: {} Year {} Month {} Day ".format(birthday[0:4],birthday[4:6],birthday[6:8]))
print(" The first place registered by the police station on the day of birth {} A {} Child ".format(sex(sex_code)[1],sex(sex_code)[0]))
print(" Check code: ",verification)
print(" Authenticity of ID number: ",verification_true_false(verification,verification_whether(target)))
Talents are shallow, and there are many deficiencies in code. Welcome to exchange and learn!
Jay Kwong