• 第七章用Python实现对Excel文件中的数据进行md5加密



    本文可以学习到以下内容:

    1. 使用 pandas 中的 to_excel 生成 excel 数据
    2. 使用 pandas 中的 read_excel 读取 excel数据
    3. 使用 pandas 中的 map 函数加密手机号的身份证号
    4. 使用 hashlib 库对数据进行 md5、sha加密

    获取数据

    数据保存在 sqlite3 数据库中,用户表 users 中保存的数据有:用户id(user_id)、用户姓名(username)、用户年龄(age)、用户手机号(mobile)以及用户身份证(idcard)。

    小凡通过下面代码获取数据库中的数据:

    import os
    import pandas as pd
    import numpy as np
    from sqlalchemy import create_engine
    
    # 数据库地址:数据库放在上一级目录下
    db_path = os.path.join(os.path.dirname(os.getcwd()), "data.db")
    engine_path = "sqlite:///" + db_path
    # 创建数据库引擎
    engine = create_engine(engine_path)
    
    sql = """
    select 
    *
    from
    users
    """
    
    df = pd.read_sql(sql, engine)
    
    # 生成excel文件
    df.to_excel("./md5加密数据.xlsx",index=False)
    # 读取生成的Excel文件
    md5_excel_df = pd.read_excel("./md5加密数据.xlsx")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    数据预览:

    在这里插入图片描述

    加密函数

    在对接不同的第三方公司,需要的加密方式也不同,小凡为了节省时间,将常用的加密方式封装为函数(encryption_str)。

    函数需要传入以下参数:加密的字符串(string)、加密算法(encry_model)、加密类型(encry_style),该函数返回加密后的字符串。

    加密函数如下:

    def encryption_str(string, encry_model="md5_32", encry_style=True):
        import hashlib
        # 加密为 utf-8 编码
        utf_8_str = str(string).encode("utf8")
        # 函数字典
        param_dict = {
            "md5_32": hashlib.md5(utf_8_str),
            "md5_16": hashlib.md5(utf_8_str),
            "sha1": hashlib.sha1(utf_8_str),
            "sha224": hashlib.sha224(utf_8_str),
            "sha256": hashlib.sha256(utf_8_str),
            "sha512": hashlib.sha512(utf_8_str)
        }
        encry_result = param_dict[encry_model].hexdigest()
        if encry_model == 'md5_16':
            encry_result = encry_result[8:-8]
        # 返回结果
        return encry_result if encry_style == "小写" else encry_result.upper()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    数据加密

    使用 pandas 中的map方法对手机号(mobile)和身份证号(idcard)列进行加密。

    md5_excel_df["mobile_md5"] = md5_excel_df["mobile"].map(lambda x:encryption_str(x))
    md5_excel_df["idcard_md5"] = md5_excel_df["idcard"].map(lambda x:encryption_str(x,"md5_16","大写"))
    
    • 1
    • 2

    数据加密结果如下:

    在这里插入图片描述

    结尾

    使用 pandas中的to_excel方法将加密后的数据保存到Excel中,代码如下:

    md5_excel_df.to_excel("./md5加密数据已完成.xlsx",index=False)
    
    • 1

    源码地址

    链接:https://pan.baidu.com/s/1rySQlwUvNkVC89qHcvbiRQ?pwd=xuyt
    提取码:xuyt

  • 相关阅读:
    【【萌新的FPGA学习之同步FIFO的代码与tb】】
    排序:归并(Merge)排序算法分析
    完美解决:sh: /usr/bin/xauth: not found
    VS Code 常用快捷键
    6.01 定时任务,关闭超时订单
    Windows安装C语言环境
    vim的使用以及配置
    SpringBoot+Netty+Vue+Websocket实现在线推送/聊天系统
    SpringMVC入门
    调用API接口的一些注意技巧
  • 原文地址:https://blog.csdn.net/weixin_42060598/article/details/126529270