python校验社会统一信用代码

# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import pymysql
SOCIAL_CREDIT_CHECK_CODE_DICT = {
                '0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,
                '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}
weighting_factor = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]
# 本体代码
def check_social_credit_code(code):
    check_code = code[17]
    # 计算校验码公式:
    #     C9 = 31-mod(sum(Ci*Wi),31),其中Ci为组织机构代码的第i位字符,Wi为第i位置的加权因子,C9为校验码
    # 第i位置上的加权因子
    ontology_code = code[0:17]
    # 计算校验码
    tmp_check_code = gen_check_code(weighting_factor, ontology_code, 31, SOCIAL_CREDIT_CHECK_CODE_DICT)
    if tmp_check_code == check_code:
        return 1
    else:
        return 0
def gen_check_code(weighting_factor, ontology_code, modulus, check_code_dict):
    #     @param weighting_factor: 加权因子
    #     @param ontology_code:本体代码
    #     @param modulus:  模数
    #     @param check_code_dict: 字符字典
    total = 0
    for i in range(len(ontology_code)):
        if ontology_code[i].isdigit():
            total += int(ontology_code[i]) * weighting_factor[i]
        else:
            total += check_code_dict[ontology_code[i]] * weighting_factor[i]
#当MOD函数值为0时,校验码用0表示
    if modulus - total % modulus == 31:
        return list(check_code_dict.keys())[list(check_code_dict.values()).index(0)]
    else:
        diff = modulus - total % modulus
        print(diff)
        return list(check_code_dict.keys())[list(check_code_dict.values()).index(diff)]

dbconn = pymysql.connect(
    host="172.16.3.23",
    database="test_srz",
    user="test_srz",
    password="test_srz123",
    port=31229,
    charset='utf8'
)
sqlcmd = "select  qymc from shtyxydmyz"
a = pd.read_sql(sqlcmd, dbconn)
zchdjh = np.array(a)
print(zchdjh)
for n in range(len(zchdjh)):
    m = zchdjh[n]
    for o in range(len(m)):
        p = m[o]
        q = check_social_credit_code(p)
        cursor = dbconn.cursor()
#第一种结果处理方法,在原表基础上增加状态
        cursor.execute("update shtyxydmyz set zt = %s where qymc ='%s'" % (q, p))
#第二种方法,合格的导入emp2,不合格的导入emp3.
        # if q==1:
        #     cursor.execute("insert into emp2(xydm) values(%s)", (p))
        # else:
        #     cursor.execute("insert into emp3(xydm) values(%s)", (p))
        dbconn.commit()
        cursor.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值