统一社会信用代码校验位python实现
2020-05-20 本文已影响0人
陈亮2019
18位统一代码最后一位计算方法如下:
```
def unioncode_checknum(code):
alpha_to_num_map={'A':10,'B':11,'C':12,'D':13,'E':14,'F':15,'G':16,'H':17,'J':18,'K':19,'L':20,'M':21,'N':22,'P':23,'Q':24,'R':25,'T':26,'U':27,'W':28,'X':29,'Y':30}
num_to_alpha_map={}
for k,v in alpha_to_num_map.items():
num_to_alpha_map[v]=k
print(num_to_alpha_map)
weights = [1,3,9,27,19,26,16,17,20,29,25,13,8,24,10,30,28]
total = 0
for i in range(len(code)):
c = code[i]
if not c.isdigit():
total+=alpha_to_num_map[c]*weights[i]
else:
total+=int(c)*weights[i]
pos = 31-(total%31)
if pos==31:
return 0
if pos<10:
return pos
else:
return num_to_alpha_map[pos]
```
统一社会信用代码定义: