python驗證身份證

2021-10-08 03:58:35 字數 1460 閱讀 7824

def get_checkcode(id):

"""計算身份證號碼的校驗位;

:param:

* id_number_str: (string) 身份證號的前17位,比如 3201241987010100

:returns:

* 返回型別 (tuple)

* flag: (bool) 如果身份證號格式正確,返回 true;格式錯誤,返回 false

* checkcode: 計算身份證前17位的校驗碼

舉例如下::

from fishbase.fish_data import *

print('--- fish_data idcard_get_checkcode demo ---')

# id number

id1 = '32012419870101001'

print(id1, idcard_get_checkcode(id1)[1])

# id number

id2 = '13052219840731647'

print(id2, idcard_get_checkcode(id2)[1])

print('---')

輸出結果::

--- fish_data idcard_get_checkcode demo ---

32012419870101001 5

13052219840731647 1

---"""

id_number_str = id[:-1]

id_end_str = id[-1:]

# 判斷長度,如果不是 17 位,直接返回失敗

if len(id_number_str) != 17:

return false

id_regex = '[1-9][0-9]([0-9][0-9x])?'

if not re.match(id_regex, id_number_str):

return false

items = [int(item) for item in id_number_str]

# 加權因子表

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()

if checkcode != id_end_str:

return false

return true

PHP驗證身份證

原作者有個錯的地方改了轉過來 document 檢查符合 gb11643 1999 標準的身份證號碼的正確性 file gb11643 1999.func.php fri mar 28 09 42 41 cst 2008 zxing updated fri mar 28 09 42 41 cst 2...

php 驗證身份證

php 根據身份證號,自動獲取對應的星座函式 function get xingzuo cid else if month 2 day 20 month 3 day 20 else if month 3 day 20 month 4 day 20 else if month 4 day 20 mon...

超實用的php驗證身份證和js驗證身份證的方法

js驗證身份證的方法 function validateidcard idcard 0 d 1 0 2 0 1 2 d 3 0 1 d 1 9 d 1 9 d 0 d 1 0 2 0 1 2 d 3 0 1 d d xx 如果通過該驗證,說明身份證格式正確,但準確性還需計算 if regidcard...