• 浔川身份证号码查询——浔川python科技社



    Python获取身份证信息

    公民身份号码是每个公民唯一的、终身不变的身份代码,由公安机关按照公民身份号码国家标准编制。每一个居民只能拥有一个唯一的身份证,它是用于证明持有人身份的一种法定证件。
    身份证包含了个人的一些重要信息,比如:

    • 前6位数字是地址码,表示发证地的行政区代码。
    • 接下来的8位数字是出生日期码,表示出生年月日。
    • 之后的3位数字是顺序码,表示在同一地址码范围内出生的人员顺序号。
    • 最后一位数字是校验码,用于检验身份证的正确性。

    除了这些基本的数字信息,身份证上还可能包含其他相关信息,如民族、性别等。这些信息可以通过相应的规则和算法进行解读和验证。


    1. # -*- coding: utf-8 -*-
    2. import tkinter as tk
    3. import tkinter.messagebox
    4. import pickle
    5. import random
    6. # 窗口
    7. window = tk.Tk()
    8. window.title('欢迎进入python')
    9. window.geometry('450x200')
    10. # 画布放置图片
    11. # canvas=tk.Canvas(window,height=300,width=500)
    12. # imagefile=tk.PhotoImage(file='qm.png')
    13. # image=canvas.create_image(0,0,anchor='nw',image=imagefile)
    14. # canvas.pack(side='top')
    15. # 标签 用户名密码
    16. Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数
    17. Verification_Code = str(Verification_Code)#把类型转换为str型
    18. print(type(Verification_Code))
    19. tk.Label(window, text='用户名:').place(x=100, y=30)
    20. tk.Label(window, text='密码:').place(x=100, y=70)
    21. tk.Label(window, text='验证码').place(x=100, y=110)
    22. tk.Label(window, text=Verification_Code).place(x=320, y=110)
    23. # 用户名输入框
    24. var_usr_name = tk.StringVar()
    25. entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
    26. entry_usr_name.place(x=160, y=30)
    27. # 密码输入框
    28. var_usr_pwd = tk.StringVar()
    29. entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
    30. entry_usr_pwd.place(x=160, y=70)
    31. #验证码输入框
    32. var_usr_yzm = tk.StringVar()
    33. entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm)
    34. entry_usr_yzm.place(x=160, y=110)
    35. # 登录函数
    36. def usr_log_in():
    37. # 输入框获取用户名密码
    38. usr_name = var_usr_name.get()
    39. usr_pwd = var_usr_pwd.get()
    40. usr_yzm = var_usr_yzm.get()
    41. #测试类型
    42. print(type(usr_yzm),type(Verification_Code))
    43. # 从本地字典获取用户信息,如果没有则新建本地数据库
    44. try:
    45. with open('usr_info.pickle', 'rb') as usr_file:
    46. usrs_info = pickle.load(usr_file)
    47. except FileNotFoundError:
    48. with open('usr_info.pickle', 'wb') as usr_file:
    49. usrs_info = {'admin': 'admin'}
    50. pickle.dump(usrs_info, usr_file)
    51. # 判断验证码是否正确用户名和密码是否匹配
    52. if usr_yzm == Verification_Code:
    53. if usr_name in usrs_info:
    54. if usr_pwd == usrs_info[usr_name]:
    55. tk.messagebox.showinfo(title='welcome',
    56. message='欢迎您:' + usr_name)
    57. else:
    58. tk.messagebox.showerror(message='密码错误')
    59. # 用户名密码不能为空
    60. elif usr_name == '' or usr_pwd == '':
    61. tk.messagebox.showerror(message='用户名或密码为空')
    62. # 不在数据库中弹出是否注册的框
    63. else:
    64. is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
    65. if is_signup:
    66. usr_sign_up()
    67. elif usr_yzm == '':
    68. tk.messagebox.showerror(message='验证码不能为空')
    69. else:
    70. tk.messagebox.showerror(message='验证码有误!')
    71. # 注册函数
    72. def usr_sign_up():
    73. # 确认注册时的相应函数
    74. def signtowcg():
    75. # 获取输入框内的内容
    76. nn = new_name.get()
    77. np = new_pwd.get()
    78. npf = new_pwd_confirm.get()
    79. # 本地加载已有用户信息,如果没有则已有用户信息为空
    80. try:
    81. with open('usr_info.pickle', 'rb') as usr_file:
    82. exist_usr_info = pickle.load(usr_file)
    83. except FileNotFoundError:
    84. exist_usr_info = {}
    85. # 检查用户名存在、密码为空、密码前后不一致
    86. if nn in exist_usr_info:
    87. tk.messagebox.showerror('错误', '用户名已存在')
    88. elif np == '' or nn == '':
    89. tk.messagebox.showerror('错误', '用户名或密码为空')
    90. elif np != npf:
    91. tk.messagebox.showerror('错误', '密码前后不一致')
    92. # 注册信息没有问题则将用户名密码写入数据库
    93. else:
    94. exist_usr_info[nn] = np
    95. with open('usr_info.pickle', 'wb') as usr_file:
    96. pickle.dump(exist_usr_info, usr_file)
    97. tk.messagebox.showinfo('欢迎', '注册成功')
    98. # 注册成功关闭注册框
    99. window_sign_up.destroy()
    100. # 新建注册界面
    101. window_sign_up = tk.Toplevel(window)
    102. window_sign_up.geometry('350x200')
    103. window_sign_up.title('注册')
    104. # 用户名变量及标签、输入框
    105. new_name = tk.StringVar()
    106. tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
    107. tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
    108. # 密码变量及标签、输入框
    109. new_pwd = tk.StringVar()
    110. tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
    111. tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
    112. # 重复密码变量及标签、输入框
    113. new_pwd_confirm = tk.StringVar()
    114. tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
    115. tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
    116. # 确认注册按钮及位置
    117. bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
    118. command=signtowcg)
    119. bt_confirm_sign_up.place(x=150, y=130)
    120. # 退出的函数
    121. def usr_sign_quit():
    122. window.destroy()
    123. # 登录 注册按钮
    124. bt_login = tk.Button(window, text='登录', command=usr_log_in)
    125. bt_login.place(x=140, y=150)
    126. bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
    127. bt_logup.place(x=210, y=150)
    128. bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
    129. bt_logquit.place(x=280, y=150)
    130. # 主循环
    131. window.mainloop()
    132. import tkinter as tk # 使用Tkinter前需要先导入
    133. import re
    134. # 第1步,实例化object,建立窗口window
    135. window = tk.Tk()
    136. # 第2步,给窗口的可视化起名字
    137. window.title('身份证号码查询')
    138. # 第3步,设定窗口的大小(长 * 宽)
    139. window.geometry('700x500')
    140. # 第4步,在图形界面上设定标签
    141. l = tk.Label(window, text='身份证号验证', font=('宋体', 30), width=40, height=2)
    142. l1 = tk.Label(window, text='身份证号:', font=('Arial', 16), width=40, height=2)
    143. # 第5步,放置标签
    144. l.place(x=-80, y=0, anchor='nw')
    145. l1.place(x=-100, y=80, anchor='nw')
    146. # 第4步,在图形界面上设定输入框控件entry框并放置
    147. e = tk.Entry(window, show=None, font=('Arial', 20),)
    148. e.place(x=200, y=90, anchor='nw')
    149. # 第5步,定义两个触发事件时的函数check和delete
    150. def check(): # 按钮'检查'对应的函数
    151. t.delete(1.0, 'end')
    152. IDcard = e.get()
    153. if len(IDcard) != 18:
    154. var = '号码:' + IDcard + "\n身份证号码位数不对!\n错误的身份证号码.\n请重新输入!\n"
    155. else:
    156. IDcard_add = IDcard[0:6] # 身份证前6位,对应归属地
    157. IDcard_birth = IDcard[6:14] # 身份证中间8位,对应出生日期
    158. IDcard_sex = IDcard[14:17] # 身份证15,,16,17位,对应性别
    159. area = {"11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁",
    160. "22": "吉林", "23": "黑龙江", "31": "上海", "32": "江苏", "33": "浙江", "34": "安徽",
    161. "35": "福建", "36": "江西", "37": "山东", "41": "河南", "42": "湖北","43": "湖南",
    162. "44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川", "52": "贵州",
    163. "53": "云南", "54": "西藏", "61": "陕西", "62": "甘肃", "63": "青海", "64": "宁夏",
    164. "65": "新疆", "71": "台湾", "81": "香港", "82": "澳门", "91": "国外"}
    165. # 地区校验
    166. if IDcard[0:2] not in area.keys():
    167. var = '号码:' + IDcard + '\n身份证地区非法!\n错误的身份证号码\n'
    168. else:
    169. year = IDcard_birth[0:4] # 出生年份
    170. month = IDcard_birth[4:6] # 出生月份
    171. day = IDcard_birth[6:8] # 出生日
    172. # 出生日期的合法性检查
    173. # 闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]
    174. # |[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
    175. # 平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]
    176. # |[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
    177. if (int(year) % 4 == 0 or (int(year) % 100 == 0 and int(year) % 4 == 0)):
    178. 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]'
    179. '|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))'
    180. '[0-9]{3}[0-9Xx]$') # //闰年出生日期的合法性正则表达式
    181. else:
    182. 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]'
    183. '|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])'
    184. ')[0-9]{3}[0-9Xx]$') # //平年出生日期的合法性正则表达式
    185. # //测试出生日期的合法性
    186. if (re.match(ereg, IDcard)):
    187. IDcard_check = IDcard[17] # 身份证最后一位
    188. W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 逐位权数
    189. IDcard_CHECK = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] # 身份证检验位
    190. IDcard_sum = 0 # 定义累加和
    191. for i in range(0, 17):
    192. IDcard_sum = IDcard_sum + int(IDcard[i]) * W[i] # 逐位加权求和
    193. IDcard_Check = IDcard_sum % 11 # 取余数
    194. if IDcard_check != IDcard_CHECK[IDcard_Check]: # 和检验位对比
    195. var = '号码:' + IDcard + '\n身份证号码校验错误!\n错误的身份证号码\n'
    196. else:
    197. if int(IDcard_sex) % 2 == 0:
    198. var = "正确的身份证号码! \n" + '号码:' + IDcard + '\n地区:' + area[IDcard[0: 2]] + \
    199. '\n生日:' + year + '年' + month + '月' + day + '日' + "\n 性别:女\n"
    200. else:
    201. var = "正确的身份证号码! \n" + '号码:' + IDcard + '\n地区:' + area[IDcard[0: 2]] + \
    202. "\n生日: " + year + '年' + month + '月' + day + '日' + "\n性别:男 \n"
    203. else:
    204. var = '号码:' + IDcard + '\n身份证号码出生日期超出范围或含有非法字符!\n错误的身份证号码\n'
    205. t.insert('insert', var) # 显示输出
    206. e.delete(0, 'end')
    207. # 第6步,创建并放置两个按钮分别触发两种情况
    208. b1 = tk.Button(window, text='检查', width=10, height=2, command = check)
    209. b1.place(x=550, y=80, anchor='nw')
    210. # 第7步,创建并放置一个多行文本框text用以显示
    211. t = tk.Text(window, font=('宋体', 20), width=38, height=8)
    212. t.place(x=100, y=210, anchor='nw')
    213. # 第8步,主窗口循环显示
    214. window.mainloop()

    运行结果展示:

    1 登录 “浔川身份证号码查询”

     2 查询身份证

  • 相关阅读:
    深度剖析:数字经济下人工智能水平的新测算模型数据集
    Kotlin 位运算
    101 - The Blocks Problem (UVA)
    手把手教你用代码画架构图
    写代码有这20个好习惯,可以减少90%非业务的bug
    CentOS操作系统的特点
    32 数据分析(下)pandas介绍
    Citus 分布式 PostgreSQL 集群 - SQL Reference(SQL支持和变通方案)
    通过Python设置及读取PDF属性,轻松管理PDF文档
    vs2022 编译遇见编译器堆空间不足,解决办法(针对CMAKE工程)
  • 原文地址:https://blog.csdn.net/2401_83104529/article/details/139701614