首页 > 其他 > 详细

校验身份证号

时间:2020-07-02 12:17:27      阅读:39      评论:0      收藏:0      [点我收藏+]
def get_checkcode(id_number_str):

id_regex = ‘[1-9][0-9]{14}([0-9]{2}[0-9X])?‘

items = [int(item) for item in id_number_str[:-1]]
# 加权因子表
factors = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
# 计算17位数字各位数字与对应的加权因子的乘积
copulas = sum([a * b for a, b in zip(factors, items)])
# 校验码表
check_codes = (‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘)
checkcode = check_codes[copulas % 11].upper()
last_str=id_number_str[-1]
print(checkcode)
if last_str==checkcode:
return True
else:
return False


if __name__ == ‘__main__‘:

id_num=‘13092619650119961X‘

print(get_checkcode(id_num))

校验身份证号

原文:https://www.cnblogs.com/fyangq/p/13223557.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!