python3 校验社会统一信用代码

https://wenku.baidu.com/view/19873704cc1755270722087c.html    

《工商行政管理注册号编制规则》


  1. # -*- coding: utf-8 -*-  
  2. import pandas as pd  
  3. import numpy as np  
  4. import pymysql  
  5. SOCIAL_CREDIT_CHECK_CODE_DICT = {  
  6.                 '0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,  
  7.                 '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,  
  8.                'R':25'T':26'U':27'W':28'X':29'Y':30}  
  9. weighting_factor = [139271926161720292513824103028]  
  10. # 本体代码  
  11. def check_social_credit_code(code):  
  12.     check_code = code[17]  
  13.     # 计算校验码公式:  
  14.     #     C9 = 31-mod(sum(Ci*Wi),31),其中Ci为组织机构代码的第i位字符,Wi为第i位置的加权因子,C9为校验码  
  15.     # 第i位置上的加权因子  
  16.     ontology_code = code[0:17]  
  17.     # 计算校验码  
  18.     tmp_check_code = gen_check_code(weighting_factor, ontology_code, 31, SOCIAL_CREDIT_CHECK_CODE_DICT)  
  19.     if tmp_check_code == check_code:  
  20.         return 1  
  21.     else:  
  22.         return 0  
  23. def gen_check_code(weighting_factor, ontology_code, modulus, check_code_dict):  
  24.     #     @param weighting_factor: 加权因子  
  25.     #     @param ontology_code:本体代码  
  26.     #     @param modulus:  模数  
  27.     #     @param check_code_dict: 字符字典  
  28.     total = 0  
  29.     for i in range(len(ontology_code)):  
  30.         if ontology_code[i].isdigit():  
  31.             total += int(ontology_code[i]) * weighting_factor[i]  
  32.         else:  
  33.             total += check_code_dict[ontology_code[i]] * weighting_factor[i]  
  34. #当MOD函数值为0时,校验码用0表示  
  35.     if modulus - total % modulus == 31:  
  36.         return list(check_code_dict.keys())[list(check_code_dict.values()).index(0)]  
  37.     else:  
  38.         diff = modulus - total % modulus  
  39.         print(diff)  
  40.         return list(check_code_dict.keys())[list(check_code_dict.values()).index(diff)]  
  41.   
  42. dbconn = pymysql.connect(  
  43.     host="122.116.345.213",  
  44.     database="test_srz",  
  45.     user="test_123",  
  46.     password="test_123",  
  47.     port=312,  
  48.     charset='utf8'  
  49. )  
  50. sqlcmd = "select  qymc from shtyxydmyz"  
  51. a = pd.read_sql(sqlcmd, dbconn)  
  52. zchdjh = np.array(a)  
  53. print(zchdjh)  
  54. for n in range(len(zchdjh)):  
  55.     m = zchdjh[n]  
  56.     for o in range(len(m)):  
  57.         p = m[o]  
  58.         q = check_social_credit_code(p)  
  59.         cursor = dbconn.cursor()  
  60. #第一种结果处理方法,在原表基础上增加状态  
  61.         cursor.execute("update shtyxydmyz set zt = %s where qymc ='%s'" % (q, p))  
  62. #第二种方法,合格的导入emp2,不合格的导入emp3.  
  63.         # if q==1:  
  64.         #     cursor.execute("insert into emp2(xydm) values(%s)", (p))  
  65.         # else:  
  66.         #     cursor.execute("insert into emp3(xydm) values(%s)", (p))  
  67.         dbconn.commit()  
  68.         cursor.close()  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值