from datetime import datetime
class IdCheck:
"""
实现身份证号校验功能
"""
def __init__(self, id_number:str):
self.id_number = id_number
self.id_list = []
self.path = "/Users/lvyz/Desktop/learnPython/testCode/idArea.txt"
self.area_list = []
self.birthday = ""
# 自测函数
self.get_id_list()
self.check_brithday()
self.check_number()
self.check_area()
def get_id_list(self):
# 区域代码
self.id_list.append(self.id_number[:6])
# 出生日期
self.id_list.append(self.id_number[6:14])
# 顺序码
self.id_list.append(self.id_number[14:17])
# 校验码
self.id_list.append(self.id_number[17:])
def check_number(self):
"""
身份证号校验
:return:
"""
if self.get_check_number() == self.id_list[-1]:
print('校验成功')
else:
print("校验失败,身份证错误")
def get_check_number(self):
"""
校验身份证号规则
前17 分别* xi_list 的每个元素之和 与11取余 在按照check_number 取到对应关系
:return: check_number[yu_number]
"""
number = self.id_number[:17]
xi_list = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
# 返回的校验码
check_number = ["1","0","X","9","8","7","6","5","4","3","2"]
sum_of_num = 0
for i in range(len(number)):
sum_of_num += int(number[i]) * xi_list[i]
yu_number = sum_of_num % 11
return check_number[yu_number]
def check_brithday(self):
"""
校验生日是否满足要求
:return:
"""
print(self.id_list[1])
a = self.id_list[1][:4]
print(a)
date = datetime(year=int(self.id_list[1][:4]),month=int(self.id_list[1][4:6]), day=int(self.id_list[1][6:]))
date_from = datetime(year=1900, month=1,day=1)
date_to = datetime.today()
if date > date_from and date < date_to:
print("生日有效")
self.birthday = self.id_list[1][:4]
return self.birthday
else:
print("生日无效")
def import_area_id(self):
"""
将文本文件读取到 列表里面
:return: self.area_list
"""
try:
with open(self.path, "r") as file:
current_line = file.readline().strip("\n")
while current_line:
current_area_list = current_line.split(" ")
# print(current_area_list)
if len(current_area_list[0]) == 6:
self.area_list.append(current_area_list)
current_line = file.readline().strip("\n")
return self.area_list
except Exception as E:
print(E)
def check_area(self):
"""
校验地区码
:return:
"""
area_list = self.import_area_id()
for i in range(len(area_list)):
if self.id_list[0] == area_list[i][0]:
print("地区校验成功,该号码属于{}地区".format(area_list[i][1]))
break
if __name__ == '__main__':
true_id = IdCheck("110102190001025618")
print("*"*100)
flase_id = IdCheck("130200188001025618")
恬淡
补充文件 idArea.txt
110000 北京市
110100 市辖区
110101 东城区
110102 西城区
110103 崇文区
110104 宣武区
110105 朝阳区
110106 丰台区
110107 石景山区
110108 海淀区
110109 门头沟区
110111 房山区
110112 通州区
110113 顺义区
110114 昌平区
110115 大兴区
110116 怀柔区
110117 平谷区
110228 密云县
110229 延庆县
网友评论