More , Please visit mine Personal blog .
2021.08.03, The COVID-19 in Wuhan seems to be showing signs of recovery , So the Wuhan government immediately took decisive measures , Start the whole city's nucleic acid detection . It is my pleasure to , Our company also actively participates in nucleic acid detection . I participated in nucleic acid experiment and data proofreading . In the process of data proofreading , The main contradiction is reflected in the mismatching of ID cards . therefore , I wrote this maybe Python The best ID card parsing tool in –parseIdCard.
according to 〖 National standard of the people's Republic of China GB11643-1999〗 The provisions on the citizenship number in , The citizenship number is the signature combination code , It consists of 17 digit body code and one digit check code . The order from left to right is : Six digit address code , Eight digit date of birth code , Three digit sequence code and one digit check code . As shown below :
42 01 16 20200103 12 3 X
The area code is relatively simple , It is the one-to-one correspondence between region and code , Just sort out the following databases .
420101 Wuhan City, Hubei Province
420102 Jiang'an District, Wuhan City, Hubei Province
420103 Jianghan District, Wuhan City, Hubei Province
420104 Qiaokou District, Wuhan City, Hubei Province
420105 Hanyang District, Wuhan City, Hubei Province
420106 Wuchang District, Wuhan City, Hubei Province
420107 Qingshan District, Wuhan City, Hubei Province
420111 Hongshan District, Wuhan City, Hubei Province
420112 Dongxihu District, Wuhan City, Hubei Province
420113 Hannan District, Wuhan City, Hubei Province
420114 Caidian District, Wuhan City, Hubei Province
420115 Jiangxia District, Wuhan City, Hubei Province
420116 Huangpi District, Wuhan City, Hubei Province
420117 Xinzhou District, Wuhan City, Hubei Province
......
Then compare the region code with the database , use Python The code implementation is as follows :
def __checkArea(areaId):
''' Calibration area . Input 6 Bit locale encoding string , return :{code, id, area}'''
if len(areaId) != 6:
return {'code': 'Error', 'id': areaId, 'area': ' Region code should be 6 position '}
else:
if areaId.isdigit():
dbAreaPath = os.path.join(os.path.dirname(__file__), 'area')
conn = sqlite3.connect(dbAreaPath)
cur = conn.cursor()
cur.execute("SELECT DISTINCT area FROM area WHERE id = '" + areaId + "'")
returnArea = ''
for c in cur:
returnArea = c[0]
conn.close()
if returnArea == '':
return {'code': 'Error', 'id': areaId, 'area': ' Unknown region code '}
else:
return {'code': 'OK', 'id': areaId, 'area': returnArea}
return {'code': 'Error', 'id': areaId, 'area': ' Illegal region code '}
as everyone knows , The date has a certain regularity . such as : The year is a four digit number , Month and day are two digits , At most, there are only 12 month , There are only... Days at most 31, But some months are 28、29、30. Put these common rules , use Python The code implementation is as follows :
def __checkBirthdate(ymd):
''' Check the date of birth . Input 8 Bit date of birth string , return :{code, id, age}'''
if len(ymd) != 8:
return {'code': 'Error', 'id': ymd, 'age': ' The date of birth should be 8 position '}
else:
if ymd.isdigit():
yearInt = int(ymd[:4])
currentYearInt = datetime.now().year
age = currentYearInt - yearInt
if age >= 0:
# Leap date :((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
# Month day of a normal year :((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
if (yearInt % 4 == 0 or yearInt % 100 == 0 and yearInt % 4 == 0):
ereg = re.compile('([1-9][0-9]{3})((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))')
else:
ereg = re.compile('([1-9][0-9]{3})((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))')
if (re.match(ereg, ymd)):
# Check by
return {'code': 'OK', 'id': ymd, 'age': age}
return {'code': 'Error', 'id': ymd, 'age': ' Illegal date of birth '}
The rules of gender check are relatively simple , The odd number is male , Even numbers are women . use Python The code implementation is as follows :
def __checkGender(gender):
''' Check gender . Input 1 Bit gender encoded string , return :{code, id, gender}'''
if len(gender) != 1:
return {'code': 'Error', 'id': gender, 'gender': ' The gender code should be 1 position '}
else:
if gender.isdigit():
genderInt = int(gender)
if genderInt % 2 == 0:
# even numbers => Woman
return {'code': 'OK', 'id': gender, 'gender': ' Woman '}
else:
# Odd number => male
return {'code': 'OK', 'id': gender, 'gender': ' male '}
else:
return {'code': 'Error', 'id': gender, 'gender': ' Illegal gender code '}
Other group codes are easy to understand , The last bit of the check code , It's more complicated . The check code is based on the first 17 digit code , according to ISO7064:1983.MOD11-2 The check code is calculated . The detailed steps are as follows :
1、 Before the ID card number. 17 The digits are multiplied by different coefficients . The coefficients from the first to the seventeenth are :7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
2、 Will this 17 The result of multiplication of digits and coefficients is added
3、 Add and divide by 11, See what the remainder is
4、 The remainder can only have 0 1 2 3 4 5 6 7 8 9 10 this 11 A digital . The corresponding check codes are 1 0 X 9 8 7 6 5 4 3 2
According to the above logic , use Python The code implementation is as follows :
def __checkJYM(inStr):
''' Check the check code . Input 17 A string , return :{code, id, area, age, gender, jym}'''
if len(inStr) != 17:
return {'code': 'Error', 'id': inStr, 'jym': ' Please enter your ID card 17 position '}
else:
if inStr.isdigit():
# Calibration area
resultArea = __checkArea(inStr[:6])
area = resultArea['area']
if resultArea['code'] == 'Error':
return {'code': 'Error', 'id': inStr, 'jym': area}
# Check the date of birth
resultBirthdate = __checkBirthdate(inStr[6:14])
age = resultBirthdate['age']
if resultBirthdate['code'] == 'Error':
return {'code': 'Error', 'id': inStr, 'jym': age}
# Check gender
resultGender = __checkGender(inStr[16:17])
gender = resultGender['gender']
if resultGender['code'] == 'Error':
return {'code': 'Error', 'id': inStr, 'jym': gender}
S = (int(inStr[0]) + int(inStr[10])) * 7 + (int(inStr[1]) + int(inStr[11])) * 9 + (int(inStr[2]) + int(inStr[12])) * 10 + (int(inStr[3]) + int(inStr[13])) * 5 + (int(inStr[4]) + int(inStr[14])) * 8 + (int(inStr[5]) + int(inStr[15])) * 4 + (int(inStr[6]) + int(inStr[16])) * 2 + int(inStr[7]) * 1 + int(inStr[8]) * 6 + int(inStr[9]) * 3
Y = S % 11
jym = '' # Check code
jymList = '10X98765432'
if Y <= 10:
jym = jymList[Y] # Check code
return {'code': 'OK', 'id': inStr, 'area': area, 'age': age, 'gender': gender, 'jym': jym}
return {'code': 'Error', 'id': inStr, 'jym': ' Illegal input parameter '}
Sum up , You can verify the overall ID card code , use Python The code implementation is as follows :
def __checkIdCard(idCard):
''' Check ID card . Input 18 Bit ID string , return :{code, id, info}'''
if len(idCard) != 18:
return {'code': 'Error', 'id': idCard, 'info': ' ID card code should be 18 position '}
else:
idCard17 = idCard[:17] # front 17 position
idCardLast = idCard[-1:] # Last
if idCard17.isdigit() and re.match('([0-9]|X|x)', idCardLast):
resultJYM = __checkJYM(idCard17)
if resultJYM['code'] == 'Error':
infoList = list()
if 'area' in resultJYM:
infoList.append(resultJYM['area'])
del resultJYM['area']
if 'age' in resultJYM:
infoList.append(resultJYM['age'])
del resultJYM['age']
if 'gender' in resultJYM:
infoList.append(resultJYM['gender'])
del resultJYM['gender']
if 'jym' in resultJYM:
infoList.append(resultJYM['jym'])
del resultJYM['jym']
resultJYM['info'] = ';'.join(infoList)
return resultJYM
if idCardLast == resultJYM['jym']:
del resultJYM['jym']
resultJYM['info'] = ' ID card verification passed '
return resultJYM
else:
return {'code': 'Error', 'id': idCard, 'info': ' The verification code of the ID card is wrong '}
else:
return {'code': 'Error', 'id': idCard, 'info': ' Illegal ID card code '}
install parseIdCard Tools , As shown below :
pip install parseIdCard
Usage method , As shown below :
from parseIdCard import parseIdCard
## Verify the area code . You can enter an integer , list , character string ( Multiple information codes can be divided equally by commas )
parseIdCard.parseArea(420116)
# Output {'code': 'OK', 'id': '420116', 'area': ' Huangpi District, Wuhan City, Hubei Province '}
parseIdCard.parseArea(['429116', '42010'])
# Output [{'code': 'Error', 'id': '429116', 'area': ' Unknown region code '}, {'code': 'Error', 'id': '42010', 'area': ' Region code should be 6 position '}]
parseIdCard.parseArea('420116,420101')
# Output [{'code': 'OK', 'id': '420116', 'area': ' Huangpi District, Wuhan City, Hubei Province '}, {'code': 'OK', 'id': '420101', 'area': ' Wuhan City, Hubei Province '}]
## Check the date of birth . You can enter an integer , list , character string ( Multiple information codes can be divided equally by commas )
parseIdCard.parseBirthdate(20200103)
# Output {'code': 'OK', 'id': '20200103', 'age': 1}
parseIdCard.parseBirthdate(['00000000', '22221203'])
# Output [{'code': 'Error', 'id': '00000000', 'age': ' Illegal date of birth '}, {'code': 'Error', 'id': '22221203', 'age': ' Illegal date of birth '}]
parseIdCard.parseBirthdate('20200103, 20121222')
# Output [{'code': 'OK', 'id': '20200103', 'age': 1}, {'code': 'OK', 'id': '20121222', 'age': 9}]
## Check gender . You can enter an integer , list , character string ( Multiple information codes can be divided equally by commas )
parseIdCard.parseGender(1)
# Output {'code': 'OK', 'id': '1', 'gender': ' male '}
parseIdCard.parseGender(['2', 'X'])
# Output [{'code': 'OK', 'id': '2', 'gender': ' Woman '}, {'code': 'Error', 'id': 'X', 'gender': ' Illegal gender code '}]
parseIdCard.parseGender('12;9')
# Output [{'code': 'Error', 'id': '12', 'gender': ' The gender code should be 1 position '}, {'code': 'OK', 'id': '9', 'gender': ' male '}]
## Calculate the check code . You can enter an integer , list , character string ( Multiple information codes can be divided equally by commas )
parseIdCard.parseJYM(42011620200103123)
# Output {'code': 'OK', 'id': '42011620200103123', 'area': ' Huangpi District, Wuhan City, Hubei Province ', 'age': 1, 'gender': ' male ', 'jym': 'X'}
parseIdCard.parseJYM(['02011620200103123', '4201162020010'])
# Output [{'code': 'Error', 'id': '02011620200103123', 'jym': ' Unknown region code '}, {'code': 'Error', 'id': '4201162020010', 'jym': ' Please enter your ID card 17 position '}]
parseIdCard.parseJYM('02011620200103123,4201162020010')
# Output [{'code': 'Error', 'id': '02011620200103123', 'jym': ' Unknown region code '}, {'code': 'Error', 'id': '4201162020010', 'jym': ' Please enter your ID card 17 position '}]
## Verify the ID card code . You can enter an integer , list , character string ( Multiple information codes can be divided equally by commas )
parseIdCard.parseIdCard(420116202001031248)
# Output {'code': 'OK', 'id': '42011620200103124', 'area': ' Huangpi District, Wuhan City, Hubei Province ', 'age': 1, 'gender': ' Woman ', 'info': ' ID card verification passed '}
parseIdCard.parseIdCard([420116202001031248, '42011620200103123X'])
# Output [{'code': 'OK', 'id': '42011620200103124', 'area': ' Huangpi District, Wuhan City, Hubei Province ', 'age': 1, 'gender': ' Woman ', 'info': ' ID card verification passed '}, {'code': 'OK', 'id': '42011620200103123', 'area': ' Huangpi District, Wuhan City, Hubei Province ', 'age': 1, 'gender': ' male ', 'info': ' ID card verification passed '}]
parseIdCard.parseIdCard('42011620200103124X;42011620200103123')
# Output [{'code': 'Error', 'id': '42011620200103124X', 'info': ' The verification code of the ID card is wrong '}, {'code': 'Error', 'id': '42011620200103123', 'gender': ' ID card code should be 18 position '}]
The subsequent version plans to add the function of intelligent repair of ID card code .
Attached is a photo of the town building where I wear protective clothing . I hope the epidemic will pass as soon as possible , Everything goes well in the future .
More programming teaching, please pay attention to the official account : Pangao will accompany you to learn programming