python身份证号码验证真伪
时间: 2023-06-13 18:08:45 浏览: 85
可以使用Python编写一个函数来验证身份证号码的真伪。身份证号码的真伪可以通过校验码进行验证,校验码是根据身份证号码前17位计算出来的。下面是一个示例代码:
```python
def check_id_number(id_number):
"""
校验身份证号码的真伪
:param id_number: 待校验的身份证号码
:return: 校验结果,True为真,False为假
"""
# 校验码对应的权值
id_weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
# 校验码对应的值
id_check = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
# 将身份证号码转换为列表
id_list = list(id_number)
# 计算校验码
id_sum = sum([int(id_list[i]) * id_weight[i] for i in range(17)])
id_mod = id_sum % 11
id_check_code = id_check[id_mod]
# 判断校验码是否正确
return id_check_code == id_list[17]
```
使用方法示例:
```python
id_number = '110101199003071234' # 待校验的身份证号码
result = check_id_number(id_number)
print(result) # 输出True或False
```
这个函数会返回一个布尔值,表示身份证号码是否正确。如果校验结果为True,则表示身份证号码正确,否则表示身份证号码错误。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)