[python]代码库
def sfz():
a4.delete(1.0, 'end')
IDcard = a2.get()
if len(IDcard) == 0:
var = IDcard + ""
elif len(IDcard) != 18:
var = IDcard + "位数错误\n请重新输入!\n"
else:
IDcard_add = IDcard[0:6]
IDcard_birth = IDcard[6:14]
IDcard_sex = IDcard[14:17]
area = {"11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁",
"22": "吉林", "23": "黑龙江", "31": "上海", "32": "江苏", "33": "浙江", "34": "安徽",
"35": "福建", "36": "江西", "37": "山东", "41": "河南", "42": "湖北","43": "湖南",
"44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川", "52": "贵州",
"53": "云南", "54": "西藏", "61": "陕西", "62": "甘肃", "63": "青海", "64": "宁夏",
"65": "新疆", "71": "台湾", "81": "香港", "82": "澳门", "91": "国外"}
if IDcard[0:2] not in area.keys():
var = IDcard + '\n地区非法或含有非法字符!'
else:
year = IDcard_birth[0:4]
month = IDcard_birth[4:6]
day = IDcard_birth[6:8]
if (int(year) % 4 == 0 or (int(year) % 100 == 0 and int(year) % 4 == 0)):
ereg = re.compile('[1-9][0-9]{5}((19[0-9]{2})|(20[0-1][0-8]))((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]))'
'[0-9]{3}[0-9Xx]$')
else:
ereg = re.compile('[1-9][0-9]{5}((19[0-9]{2})|(20[0-1][0-8]))((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])'
')[0-9]{3}[0-9Xx]$')
if (re.match(ereg, IDcard)):
IDcard_check = IDcard[17]
W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
IDcard_CHECK = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
IDcard_sum = 0
for i in range(0, 17):
IDcard_sum = IDcard_sum + int(IDcard[i]) * W[i]
IDcard_Check = IDcard_sum % 11
if IDcard_check != IDcard_CHECK[IDcard_Check]:
var = IDcard + '\n校验码错误!'
else:
if int(IDcard_sex) % 2 == 0:
var = IDcard + '\n地区:' + area[IDcard[0: 2]] + \
'\n生日:' + year + '年' + month + '月' + day + '日' + "\n 性别:女\n"
else:
var = IDcard + '\n地区:' + area[IDcard[0: 2]] + \
"\n生日: " + year + '年' + month + '月' + day + '日' + "\n性别:男 \n"
else:
var = IDcard + '\n出生日期超出范围或含有非法字符!'
a4.insert('insert', var)
a2.delete(0, 'end')
from tkinter import *
w =Tk()
w.title('身份证号码解析校验器')
w.geometry('420x200')
a1= Label(w, text='输入身份证号码:',\
font=('宋体', 11),\
width=14)
a2= Entry(w,bg='white',\
fg='black',\
font=('仿宋',14),\
width=18)
a3= Button(w, text='确定',\
width=12,\
command = sfz)
a4= Text(w, font=('宋体', 11),\
width=14,\
height=8)
a5= Label(w,text='LH赫公司出品\nQQ:2643661328',\
font=('宋体', 11),\
width=14)
a1.grid(column=1,row=1)
a2.grid(column=2,row=1)
a3.grid(column=2,row=2)
a4.grid(column=2,row=3)
a5.grid(column=3,row=1)
#2.0版本更新日志
'''
1.优化了位数判断逻辑
2.丰富了界面内容
'''
[代码运行效果截图]
初级程序员
by: 尾翼稳定脱壳穿甲弹 发表于:2023-02-05 15:20:00 顶(0) | 踩(0) 回复
注意,本源码需要在3.8环境运行,高版本会报错
回复评论