Skip to content

Commit a26122a

Browse files
[投稿] 添加脚本: 身份证核验 — by 霞飞路
1 parent 7dc35a0 commit a26122a

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import re
2+
def check_id_card(id_str):
3+
if len(id_str) != 18 : return False
4+
last = id_str[17].upper()
5+
if not (last.isdigit() or last == 'X') : return False
6+
try:
7+
if not re.search(r"^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$",id_str).group():pass
8+
except:
9+
return False
10+
weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
11+
codes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
12+
total = sum(int(id_str[i]) * weights[i] for i in range(17))
13+
mod = total % 11
14+
expected = codes[mod]
15+
return last == expected
16+
if __name__ == "__main__":
17+
print(check_id_card(input("输入18位完整sfz号码:")))

0 commit comments

Comments
 (0)