python 批量校验身份证是否合法

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

python 连接数据库

import traceback

import pymysql as pymysql


class localhostDb(object):
    def __init__(self):
        self.conn = pymysql.connect(host='localhost',
                                    user='root',
                                    password='root',
                                    #db='',
                                    port=3306,
                                    charset='utf8',
                                    cursorclass=pymysql.cursors.DictCursor,
                                    connect_timeout=60)
    
    def insert_sql(self, sql, data):
        cursor = self.conn.cursor()
        row = 0
        try:
            row = cursor.executemany(sql, data)
            self.conn.commit()
        except:
            self.conn.rollback()
            traceback.print_exc()
        finally:
            cursor.close()
        return row
    
    def find_fetchall(self,sql):
        cursor = self.conn.cursor()
        results = ''
        try:
            cursor.execute(sql)
            results = cursor.fetchall()
        except:
            print("Error: unable to fecth data")
        finally:
            cursor.close()
            self.conn.close()
        return results

校验身份证

import sys

from mydb.localhostDb import localhostDb

#输入身份证检测就是否合法
def checkOnLineInput():
    factor = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
    last = ("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2")
    while True:
        flag = True
        ident = input("请输入你的身份证号")
        ident = ident.upper()
        # 首先判断输入的是否为18位
        if len(ident) == 18:
            # 除去字符串中的空格
            identity = ident.replace(" ", "")
            if len(identity) == 18:
                identity_result = []
                # 判断前17位是否有非法字符
                for i in identity[0: 17: 1]:
                    try:
                        identity_result.append(int(i))
                    except:
                        flag = False

                # 使用lambda匿名函数定义乘法规则
                func = lambda x, y: x * y
                # map内置函数会根据提供的函数对指定的序列做映射,得到两个列表相乘的列表
                result_mult = list(map(func, identity_result, factor))

                # 将相乘的结果相加用相加的结果与11求模
                result_add_yu = sum(result_mult) % 11

                # 根据身份证的第十八位和求得的余数对应的字符判断身份证号是否合法
                if identity[17] == last[result_add_yu] and flag == True:
                    print("身份证号输入合法。")
                    if identity_result[16] % 2 == 0:
                        print("性别为女性。")
                    else:
                        print("性别为男性。")
                    break
                else:
                    print("身份证号输入不合法请重新输入!")
                    continue
            else:
                print("身份证号位数不合法请重新输入")
                continue
        else:
            print("身份证号位数不合法请重新输入")
            continue

#输入身份证检测就是否合法
def check(ident):
    factor = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
    last = ("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2")
    ident = ident.upper()
    # 首先判断输入的是否为18位
    flag = True
    if len(ident) == 18:
        # 除去字符串中的空格
        identity = ident.replace(" ", "")
        if len(identity) == 18:
            identity_result = []
            # 判断前17位是否有非法字符
            for i in identity[0: 17: 1]:
                try:
                    identity_result.append(int(i))
                except:
                    flag = False

            # 使用lambda匿名函数定义乘法规则
            func = lambda x, y: x * y
            # map内置函数会根据提供的函数对指定的序列做映射,得到两个列表相乘的列表
            result_mult = list(map(func, identity_result, factor))

            # 将相乘的结果相加用相加的结果与11求模
            result_add_yu = sum(result_mult) % 11

            # 根据身份证的第十八位和求得的余数对应的字符判断身份证号是否合法
            if identity[17] == last[result_add_yu] and flag == True:
                # print("身份证号输入合法。")
                # if identity_result[16] % 2 == 0:
                #     print("性别为女性。")
                # else:
                #     print("性别为男性。")
                # break
                return True
            else:
                #print("身份证号输入不合法请重新输入!")
                return not True
        else:
            #print("身份证号位数不合法请重新输入")
            return not True
    else:
        #print("身份证号位数不合法请重新输入")
        return not True




def checkIdFromDb(limit,page):
    localSql = "select id,minwen_id_card_no  from `pangu`.`uc_user` where minwen_id_card_no is not null and  is_right_id_card_no is null and id>="+str(page*limit)+"  and id<"+str((page+1)*limit)+" limit " + str(limit)
    # print(localSql)
    # pass
    localDb2 = localhostDb()
    localDb2cursor = localDb2.conn.cursor()
    localDb2cursor.execute(localSql)
    localhostResult = localDb2cursor.fetchall()
    if localhostResult is not None:
        for item in localhostResult:
            ch = check(item['minwen_id_card_no'])
            if(ch):
                checkRe = 1
            else:
                checkRe = 0

            updateSql = "update  `pangu`.`user` set  is_right_id_card_no ='" + str(checkRe) \
                        + "' where id = " + str(item['id']) + " limit 1"
            print(updateSql)
            localDb2cursor.execute(updateSql)
        localDb2.conn.commit()
    else:
        print("no datas")
        sys.exit()

if __name__ == "__main__":

    limit = 1000
    page = 1
    while True:
        checkIdFromDb(limit,page)
        page += 1
        print(page*limit)
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: python