校验身份证号

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