首页 > 编程语言 >python处理Excel文件的导入

python处理Excel文件的导入

时间:2023-11-20 14:44:36浏览次数:38  
标签:val python company Excel label cursor field 导入 id

  1. 处理的文件:
    label.xlsx

    AP ID Group Borrower【Holding Company ID】 Group Borrower【Type of Company】 Watermark 3.0 (Migration client only)【Effective Date】
    1268 1974 Affiliate  
    1741 1268 Holding Company  
    1890 2073 Affiliate 2023/12/30
    1955 2136 Affiliate  
    2008     2023/11/12
    2073 1890 Holding Company  
  2. 新建文件夹inputFiles,把上面的文件放入到对应的文件夹里面:
  3. 实现代码导入数据到label相关数据表:

    import math
    import os
    import pandas as pd
    import pymysql.cursors
    from datetime import datetime
    
    # 数据库连接配置
    db_config = {
        'host': 'your_database_host',
        'user': 'your_database_user',
        'password': 'your_database_password',
        'database': 'your_database_name'
    }
    
    # 连接到SQLite数据库,替换为你的实际数据库连接信息
    connection = pymysql.connect (host=db_config['host'], user=db_config['user'], password=db_config['password'],
                                  database=db_config['database'], cursorclass=pymysql.cursors.DictCursor)
    
    # 定义文件夹名称
    input_folder = 'inputFiles'
    
    # 定义 Excel 文件的名称
    excel_file_name = 'label.xlsx'
    
    # 拼接完整路径
    excel_file_path = os.path.join (input_folder, excel_file_name)
    
    # 读取 Excel 文件到数据框架
    df = pd.read_excel (excel_file_path)
    
    
    # 在onboard_companies表中查找onboard_company_id
    def find_onboard_company_id(ap_id):
        # 查询数据库
        with connection.cursor () as cursor:
            cursor.execute ("SELECT id FROM onboard_companies WHERE ap_cid=%s AND status=%s", (ap_id, 1))
            result = cursor.fetchone ()
            return result['id'] if result else None
    
    
    # 在labels表中查找label_id
    def find_label_id(label_name):
        # 查询数据库
        with connection.cursor () as cursor:
            cursor.execute ("SELECT id FROM labels WHERE name=%s AND status=%s", (label_name, 2))
            result = cursor.fetchone ()
            return result['id'] if result else None
    
    
    # 在label_fields表中查找label_field_id
    def find_label_field_id(label_field_name, label_id):
        # 查询数据库
        with connection.cursor () as cursor:
            cursor.execute ("SELECT id FROM label_fields WHERE name=%s AND label_id=%s AND status=1",
                            (label_field_name, label_id))
            result = cursor.fetchone ()
            return result['id'] if result else None
    
    
    # 在company_labels表中查找数据
    def find_company_label(onboard_company_id, label_id):
        # 查询数据库
        with connection.cursor () as cursor:
            cursor.execute ("SELECT id FROM company_labels WHERE onboard_company_id=%s AND label_id=%s AND status=1",
                            (onboard_company_id, label_id))
            result = cursor.fetchone ()
            # 返回包含查询结果的字典,或者返回None表示未找到
            return result if result else None
    
    
    # 在company_labels表中创建新数据
    def create_company_label(onboard_company_id, label_id):
        # 向数据库插入新数据
        with connection.cursor () as cursor:
            current_time = datetime.now ().strftime ("%Y-%m-%d %H:%M:%S")
            cursor.execute ("INSERT INTO company_labels (onboard_company_id, label_id, created_by, updated_by, "
                            "company_label_status, status, created_at, updated_at) "
                            "VALUES (%s, %s, 0, 0, 1, 1, %s, %s)",
                            (onboard_company_id, label_id, current_time, current_time))
            connection.commit ()
            # 返回包含新数据ID的字典
            return cursor.lastrowid
    
    
    # 在company_label_vals表中查找或创建数据
    def find_label_vals(company_label_id, label_field_id):
        # 查询或创建数据
        with connection.cursor () as cursor:
            cursor.execute (
                "SELECT id FROM company_label_vals WHERE company_label_id=%s AND label_field_id=%s AND status=1",
                (company_label_id, label_field_id))
            result = cursor.fetchone ()
            return result if result else None
        pass
    
    
    # 更新已存在的company_label_vals
    def update_label_val(val_id, val):
        with connection.cursor () as cursor:
            current_time = datetime.now ().strftime ("%Y-%m-%d %H:%M:%S")
            cursor.execute ("UPDATE company_label_vals SET val=%s, updated_at=%s WHERE id=%s", (val, current_time, val_id))
            connection.commit ()
    
    
    # 新加数据到company_label_vals
    def insert_label_val(company_label_id, label_field_id, val):
        with connection.cursor () as cursor:
            current_time = datetime.now ().strftime ("%Y-%m-%d %H:%M:%S")
            cursor.execute (
                "INSERT INTO company_label_vals (company_label_id, label_field_id, val, status, created_at, updated_at) "
                "VALUES (%s, %s, %s, 1, %s, %s)", (company_label_id, label_field_id, val, current_time, current_time))
            connection.commit ()
    
            # 数据库操作的函数
    
    
    def find_or_create_company_label(onboard_company_id, label_id, label_field_id, val):
        # 在company_labels表中查找对应数据
        company_label_data = find_company_label (onboard_company_id, label_id)
        if company_label_data:
            company_label_id = company_label_data['id']
        else:
            # 在company_labels表中创建新数据
            company_label_id = create_company_label (onboard_company_id, label_id)
    
        # 在company_label_vals表中查找或创建数据
        company_label_vals_data = find_label_vals (company_label_id, label_field_id)
        if company_label_vals_data:
            val_id = company_label_vals_data['id']
            # 在company_label_vals表中更新数据
            update_label_val (val_id, val)
        else:
            # 在company_label_vals表中创建新数据
            insert_label_val (company_label_id, label_field_id, val)
    
    
    # 遍历Excel表格的每一行
    for index, row in df.iterrows ():
        ap_id = row['AP ID']
        val1 = row['Group Borrower【Holding Company ID】']
        val2 = row['Group Borrower【Type of Company】']
        val3 = row['Watermark 3.0 (Migration client only)【Effective Date】']
    
        # 使用 pd.isnull() 检查是否是 NaN 或 NaT
        is_nan_val1 = pd.isnull (val1)
        is_nan_val2 = pd.isnull (val2)
        is_nan_val3 = pd.isnull (val3)
    
        onboard_company_id = find_onboard_company_id (ap_id)
        if onboard_company_id:
            if is_nan_val1:
                print (f"{row['AP ID']} 'Group Borrower【Holding Company ID】' Value is NaN or Nat , skipping...")
            else:
                label_id_holding = find_label_id ("Group Borrower")
                label_field_id_holding = find_label_field_id ("Holding Company ID", label_id_holding)
                val1_as_int = int (val1)
                val_with_quotes = f'"{val1_as_int}"'  # 将val添加双引号
                find_or_create_company_label (onboard_company_id, label_id_holding, label_field_id_holding, val_with_quotes)
    
            if is_nan_val2:
                print (f"{row['AP ID']} 'Group Borrower【Type of Company】' Value is NaN or Nat , skipping...")
            else:
                label_id_type = find_label_id ("Group Borrower")
                label_field_id_type = find_label_field_id ("Type of Company", label_id_type)
                val_with_quotes = f'"{val2}"'  # 将val添加双引号
                find_or_create_company_label (onboard_company_id, label_id_type, label_field_id_type, val_with_quotes)
    
            # 检查是否为 NaT
            if is_nan_val3:
                print (
                    f"{row['AP ID']} 'Watermark 3.0 (Migration client only)【Effective Date】' Value is NaN or Nat , skipping...")
            else:
                # 将 datetime 对象转换为字符串,使用指定的日期格式
                formatted_date_str = val3.strftime ("%Y-%m-%d")
                label_id_effective_date = find_label_id ("Watermark 3.0 (Migration client only)")
                label_field_id_effective_date = find_label_field_id ("Effective Date", label_id_effective_date)
                val_with_quotes = f'"{formatted_date_str}"'  # 将val添加双引号
                find_or_create_company_label (onboard_company_id, label_id_effective_date, label_field_id_effective_date,
                                              val_with_quotes)
        else:
            print (f"AP ID {ap_id} 对应的 onboard_company_id 未找到.")
    

      

标签:val,python,company,Excel,label,cursor,field,导入,id
From: https://www.cnblogs.com/kaka0318/p/17843907.html

相关文章

  • python处理数据的导出到Excel
    importdatetimeimportjsonimportosimportpandasaspdfromsqlalchemyimportcreate_enginefromsqlalchemy.sqlimporttext#数据库连接配置,请根据你的实际情况修改db_config={'host':'your_database_host','user':'your_data......
  • python2和3的语法区别
    `` - python2相当于repr  python3不能使用Input-python2如果输入字符需要加双引号,数字不需要加 python3数字字符都可以Raw_input-python2输入数字字符都可以    python3丢弃exceptException,e:- python2可以用  python3语法报错,推荐使用exceptE......
  • Selenium4+python被单独定义<div>的动态输入框和二级下拉框要怎么定位?
    今天在做练习题的时候,发现几个问题捣鼓了好久,写下这篇来记录问题一:有层级的复选框无法定位到二级目录 对于这种拥有二级框的选项无法定位,也不是<select>属性.我们查看下HTML,发现它是被单独封装在body内拥有动态属性的独立<div>,当窗口点击的时候才会触发. 解......
  • 加密锁(加密狗)保护Python应用程序防止被反编译
    开发套件使用的是原HASP/SafeNet/金雅拓/泰雷兹加密狗最新开发工具加密锁(加密狗)对python程序代码加密保护,保护自己软件版权被F法使用。(注:本文为Python程序保护的纯技术贴,欢迎留言探讨)开发工具下载:​​http://www.chinadlp.com/?list-DriveDownload.html​​概述使用圣天诺加密......
  • [944] Extracting tables from a PDF in Python
    ToextracttablesfromaPDFinPython,wecanuseseverallibraries.Onepopularchoiceisthe tabula-pylibrary,whichisaPythonwrapperforApachePDFBox.Hereisastep-by-stepguidetogetstarted:1.Installtherequiredlibraries:pipinstalltab......
  • 【4.0】Python高级之匿名函数
    【一】匿名函数的定义在Python里有两类函数:第一类:用def关键词定义的正规函数第二类:用lambda关键词定义的匿名函数Python使用lambda关键词来创建匿名函数,而非def关键词lambdaargument_list:expressionlambda:定义匿名函数的关键词。argument_list:函......
  • 【7.0】Python高级之生成器
    【一】什么是生成器?Python中的生成器是一种特殊的迭代器,可以在需要时生成数据,而不必提前从内存中生成并存储整个数据集。通过生成器,可以逐个生成序列中的元素,而无需一次性生成整个序列。生成器在处理大数据集时,具有节省内存、提高效率的特点。【二】生成器有两种创建方式【......
  • 【6.0】Python高级之迭代器
    【一】迭代器介绍迭代器即用来迭代取值的工具,而迭代是重复反馈过程的活动其目的通常是为了逼近所需的目标或结果,每一次对过程的重复称为一次“迭代”而每一次迭代得到的结果会作为下一次迭代的初始值,单纯的重复并不是迭代whileTrue:msg=input('>>:').strip()......
  • sass的模块导入
    sass的模块导入:https://blog.csdn.net/iamdidi0110/article/details/131416901?ops_request_misc=&request_id=&biz_id=102&utm_term=%E5%BC%95%E5%85%A5sass%E6%96%87%E4%BB%B6&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default......
  • EXCEL中逆向查找的十种方法
    逆向查找在Excel中指的是根据某个数值或条件,查找该数值或条件所在的单元格位置。逆向查找可以帮助用户快速定位数据,对于数据分析和处理非常有用。下面将详细介绍在Excel中进行逆向查找的十种方法。一、使用MATCH函数MATCH函数可以在指定范围内查找具体数值或条件,并返回该数值或......