[Python] validity test of ID number

XavierJ 2021-01-21 06:06:01
python validity test id number


1. Preface

National standard of the people's Republic of China GB 11643-1999《 Citizenship number 》 Specified in the : The citizenship number is the signature combination code , from 17 Bit number ontology code and 1 Bit check code composition .

18 What's the way of bit number combination :
 Insert picture description here
among , The last bit is the check code , It is from the front 17 The number of bits is calculated in some way . The check code is one digit , But if the last check code is used, the check code calculated by the system is “10”, The ID number is 18 The regulation of bit , with “X” Substitution check code “10”.

The calculation method is as follows :

  1. Mark the ID number from left to right. a 1 , a 2 , ⋯ , a 18 \displaystyle a_{1},a_{2},\cdots ,a_{18} a1,a2,,a18; a 18 \displaystyle a_{18} a18 It is the check code ;
  2. Calculate the weight coefficient W i = 2 18 − i   m o d   11 \displaystyle W_{i}=2^{18-i}\ {\bmod {\ }}{11} Wi=218i mod 11; among   m o d   \displaystyle \ \bmod {\ }  mod  Represents the remainder .
i i i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
W i W_{i} Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
  1. Calculation S = ∑ i = 1 17 a i ⋅ W i \displaystyle S=\sum _{i=1}^{17}a_{i}\cdot W_{i} S=i=117aiWi
  2. a 18 = ( 12 − ( S   m o d 1 1 ) ) m o d 1 1 \displaystyle a_{18}=(12-(S\ {\bmod {1}}1)){\bmod {1}}1 a18=(12(S mod11))mod11

2. Code

def main(id):
sum = 0
for index, item in enumerate(id[:-1]):
sum += 2**(17-index) % 11*int(item)
num = (12 - sum % 11) % 11
if num < 10:
return ' Check by ' if id[-1] == str(num) else f' Check failed , The correct ending number should be :{num}'
else:
return ' Check by ' if id[-1] == 'X' else f' Check failed , The correct ending number should be :{num}'
if __name__ == '__main__':
test_id_1 = '532527195503267352'
test_id_2 = '53252719550326735X'
print(main(test_id_1))
print(main(test_id_2))
''' Output
Check by
Check failed , The correct ending number should be :2
'''

3. Be careful

The correct check code is the necessary condition for the validity of ID number. , Not sufficient conditions . To ensure the validity of ID number. , Other conditions should also be met , For example, the number of digits must be 18 position , The address code shall comply with the provisions of the people's Republic of China on the code of administrative divisions , Also, the birth date code should be legal and so on . If you are interested , You can program it yourself . Of course , Here is also a packaged one python Third party Library id-validator.

3.1. install

pip install id-validator

3.2. Use

3.2.1. Verify the legitimacy of ID number.

Verify that ID number is legal. , Legal return True, Illegal return False:

>>> from id_validator import validator
>>> validator.is_valid('440308199901101512')
True
>>> validator.is_valid('44030819990110151X')
False

3.2.2. Get ID number information

When the ID number is legal , Return analysis information ( region 、 Date of birth 、 The constellation 、 the Chinese zodiac 、 Gender 、 Check bit ), Illegal return False:

>>> from id_validator import validator
>>> validator.get_info('440308199901101512')
{

"address_code": "440308",
"abandoned": 0,
"address": " Yantian District, Shenzhen City, Guangdong Province ",
"address_tree": [
" Guangdong province, ",
" shenzhen ",
" Yantian district "
],
"age": 22,
"birthday_code": "1999-01-10",
"constellation": " Capricornus ",
"chinese_zodiac": " Mao rabbit ",
"sex": 1,
"length": 18,
"check_bit": "2"
}

3.2.3. Generate false data that can be verified

Forgery of identity card in accordance with verification :

>>> from id_validator import validator
>>> validator.fake_id()
'410704200302268552'

4. Reference resources

https://zh.wikipedia.org/wiki/ Citizenship number of the people's Republic of China
https://github.com/jxlwqq/id-validator.py

This article is shared in Blog “Xavier Jiezou”(CSDN).
If there is any infringement , Please contact the support@oschina.cn Delete .
Participation of this paper “OSC Source creation plan ”, You are welcome to join us , share .

版权声明
本文为[XavierJ]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/01/20210121060356587b.html

  1. 【七天搞定Python】day01.Python环境配置、pip、IDE、注释、变量,数据类型、标识符/关键字、输出、输入
  2. Life is short, I learn Python
  3. Python image enhancement and special effects - using Baidu AI to color black and white images
  4. Python environment configuration, Pip, IDE, comment, variable, data type, identifier / keyword, output, input
  5. 为什么说Python是最伟大的语言?看图就知道了 - 知乎
  6. Why is Python the greatest language? Just look at the picture. - Zhihu
  7. 通过创建视频游戏来学习 Python
  8. Learn Python by creating video games
  9. Python3版本下创建计算给定日期范围内工作日方法
  10. Creating a method to calculate working days within a given date range in Python 3
  11. 图解爬虫,用几个最简单的例子带你入门Python爬虫
  12. Graphical crawler, with a few of the simplest examples to take you to the introduction of Python crawler
  13. python+requests基础知识
  14. Basic knowledge of Python + requests
  15. python自定义windowsr日志支持文件分割
  16. python+requests基础知识
  17. Python custom Windowsr log supports file segmentation
  18. Basic knowledge of Python + requests
  19. 高级测试 | Python笔试题
  20. 火了!开源的 Python 抢票神器,过年回家就看这一波了!
  21. Python 爬虫进阶 - 前后端分离有什么了不起,过程超详细!
  22. 【python】使用pip提示ModuleNotFoundError
  23. 【python】虚拟环境搭建
  24. Advanced test | Python written test questions
  25. Fire! Open source Python ticket grabbing artifact, come home to see this wave of New Year!
  26. Python crawler advanced - before and after the end of the separation of what great, super detailed process!
  27. [Python] prompt modulenotfounderror with PIP
  28. Building a virtual environment
  29. Serverless 架构下用 Python 轻松搞定图像分类和预测
  30. Easy image classification and prediction with Python under serverless architecture
  31. python协程爬取某网站的老赖数据
  32. Python coroutine crawls Laolai data of a website
  33. 使用Python分析姿态估计数据集COCO的教程
  34. Using Python to analyze the data set coco of attitude estimation
  35. win环境 python3 flask 上手整理 环境搭建(一)
  36. Getting started with win environment python3 flash
  37. Python实现一个论文下载器,赶紧收藏
  38. win环境 python3 flask 上手整理 快速上手-基础操作(二)
  39. Python 中常见的配置文件写法
  40. Python to achieve a paper Downloader, quickly collect
  41. Python批量 png转ico
  42. 使用line_profiler对python代码性能进行评估优化
  43. 使用line_profiler对python代码性能进行评估优化
  44. Getting started with Python 3 flash in win environment
  45. Common ways to write configuration files in Python
  46. Python会在2021年死去吗? Python 3.9最终版本的回顾
  47. Python batch PNG to ICO
  48. Using line_ Profiler evaluates and optimizes the performance of Python code
  49. Using line_ Profiler evaluates and optimizes the performance of Python code
  50. Will Python die in 2021? A review of the final version of Python 3.9
  51. Python3 SMTP send mail
  52. Understanding closures in Python: getting started with closures
  53. Python日志实践
  54. Python logging practice
  55. [python opencv 计算机视觉零基础到实战] 十、图片效果毛玻璃
  56. [python opencv 计算机视觉零基础到实战] 九、模糊
  57. 10. Picture effect ground glass
  58. [Python opencv computer vision zero basis to actual combat] 9. Fuzzy
  59. 使用line_profiler對python程式碼效能進行評估優化
  60. Using line_ Profiler to evaluate and optimize the performance of Python code