根据GB11643-1999公民身份证号码是特征组合码,由十七位数字本体码和一位数字校验码组成,排列顺序从左至右依次为:
- 六位数字地址码
- 八位数字出生日期码
- 三位数字顺序码
- 一位数字校验码(数字10用罗马X表示)
校验码采用ISO7064:1983,MOD11-2校验码系统(图为校验规则样例)
用身份证号的前17位的每一位号码字符值分别乘上对应的加权因子值,得到的结果求和后对11进行取余,最后的结果放到表2检验码字符值..换算关系表中得出最后的一位身份证号码
import datetime
def return_ditu():
path = 'C:/Users/2019/Desktop/身份证/行政划分.txt'
f = open(path,'r', encoding="utf-8")
dict_sheng = {}
dict_shi = {}
dict_diqu = {}
dict_all = {}
for sig in f.readlines():
sig = sig.replace(" ","")
sig = sig.replace("\n","")
# print(sig[:6],sig[6:])
if '000' in sig:
# print("我是省和直辖市{}".format(sig[:-1]))
dict_sheng[sig[:6]] = sig[6:]
else:
if '00' in sig:
# print("我是市{}".format(sig[:-1]))
dict_shi[sig[:6]] = sig[6:]
else:
# print("我是地区{}".format(sig[:-1]))
dict_diqu[sig[:6]] = sig[6:]
for i in dict_diqu:
cahe_sheng= i[:-4]+'0000'
cahe_shi = i[:-2]+'00'
if cahe_shi in dict_shi:
dict_all[i] = dict_sheng[cahe_sheng],dict_shi[cahe_shi],dict_diqu[i]
# print(i,dict_sheng[cahe_sheng],dict_shi[cahe_shi],dict_diqu[i])
else:
dict_all[i] = dict_sheng[cahe_sheng],dict_diqu[i]
# print(i,dict_sheng[cahe_sheng],dict_diqu[i])
return dict_all
def get_where(id_card):
where_id = id_card[:6]
print("籍贯:",end=" ")
print(area_id[where_id])
def check_where(id_card):
where_id = id_card[:6]
try:
are = area_id[where_id]
return 1
except KeyError:
return error_type[0]
# print("找不到该地区或身份证前六位有误")
def get_birth(id_card):
birth_id = id_card[6:14]
print("出生日期:{}".format(birth_id))
def get_sex(id_card):
sex_id = id_card[-2:-1]
print("性别: ",end=" ")
if int(sex_id) % 2 == 0:
print("女")
else:
print("男")
def get_tureorerror(id_card):
sum = 0
list_quan = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1]
list_ture = [1,0,'X',9,8,7,6,5,4,3,2]
for idx in range(0,len(list_quan)-1):
cach = list_quan[idx]*int(id_card[idx])
sum = sum + cach
last = list_ture[sum % 11]
if str(last) == id_card[-1:]:
print("正确身份证")
return error_type[4]
else:
return error_type[2]
def check_id_card(id_card):
if len(id_card) != 18:
print("长度错误")
return error_type[3]
else:
try:
res = datetime.datetime(int(id_card[6:10]), int(id_card[10:12]), int(id_card[12:14]))
except ValueError:
print("出生日期有误")
return error_type[1]
sig = check_where(id_card)
if sig != 1:
return sig
else:
return get_tureorerror(id_card)
if __name__ == '__main__':
error_type = ['找不到该地区或身份证前六位错误', '出生日期错误', '身份证校验错误', '身份证长度错误','正确身份证']
area_id = return_ditu()
# id = '130828199807160612'
id = input("请输入身份证号码:")
res = check_id_card(id)
if res == '正确身份证':
get_where(id)
get_sex(id)
get_birth(id)
else:
print(res)
使用说明:
运行前请先复制到行政规划到指定txt文件,并修改path中txt文件存放路径
行政规划链接:https://blog.csdn.net/qq_42006613/article/details/109625058