python excel 读取及写入固定格式

复制代码
import xlrd
import xlwt
import re
import pandas as pd
from datetime import date,datetime

file_path = "C:\\Users\\function_model.xls"
def readexcel():
    df = pd.read_excel(file_path ,"配置")

    # e_id
    # id
    # expression
    # name
    # freq
    # column_data = df['e_id']

    excel_date = df.loc[:, ['e_id', 'id','expression', 'name', 'freq']]
    str1= 'rDataV'
    str2 = 'fillMissingDates'
    str3 = 'jactor_value'
    history3 = 'quantile(B'
    trend_judgement = 'judgement('
    marginal_trend_media = 'trend_media('

    my_array = []
    list_name = ["e_id", "expression_index", "expression_name", "expression_factor_freq",
                  "expression_factor_id", "expression_factor_is_industry", "expression_name"]
    my_array.append(list_name)


    for index, row in excel_date.iterrows():
        expression = row['expression']
        e_id = row['e_id']
        id = row['id']
        name = row['name']
        freq = row['freq']
        if str1 in expression:
            list_index=[e_id ,0 ,  "B" ,freq ,0 ,0 ,"仅占位"]
            my_array.append(list_index)

        elif str2 in expression:
            pattern = r'\b\d{12}\b'
            matches = re.findall(pattern, expression)
            list_index = [e_id, 0, "B", freq, matches[0], 1, name[:-4]]
            my_array.append(list_index)
        elif marginal_trend_media in expression:
            pattern = r'\b\d{12}\b'
            matches = re.findall(pattern, expression)
            list_index = [e_id, 0, "B", freq, matches[0], 1, name[:-5]]
            my_array.append(list_index)
        elif history3 in expression:
            pattern = r'\b\d{12}\b'
            matches = re.findall(pattern, expression)
            list_index = [e_id, 0, "B", freq, matches[1], 1, name[:-5]]
            my_array.append(list_index)
        elif trend_judgement in expression:
            pattern = r'\b\d{12}\b'
            matches = re.findall(pattern, expression)
            list_index = [e_id, 0, "B", freq, matches[1], 1, name[:-5]]
            my_array.append(list_index)

        elif str3 in expression:
            list_index = [e_id, 0, "B", freq, 0, 0, "占位"]
            my_array.append(list_index)
        else:
           list_index = [e_id, 0, "B", freq, "待填充", 1, "待填充"]
           my_array.append(list_index)
    print(my_array)
    # 将数组转化为DataFrame
    dd = pd.DataFrame(my_array)
    dd.to_excel('配置.xlsx', index=False, header=False)

if __name__ == '__main__':
    readexcel()
相关推荐
信奥卷王33 分钟前
[GESP202503 五级] 原根判断
java·数据结构·算法
haoly19895 小时前
数据结构和算法篇-线性查找优化-移至开头策略
数据结构·算法·移至开头策略
earthzhang20218 小时前
【1007】计算(a+b)×c的值
c语言·开发语言·数据结构·算法·青少年编程
冷徹 .12 小时前
2024ICPC区域赛香港站
数据结构·c++·算法
韧竹、13 小时前
数据结构之顺序表
c语言·数据结构
非凡ghost17 小时前
猫眼浏览器(Chrome内核增强版浏览器)官方便携版
前端·网络·chrome·windows·软件需求
努力努力再努力wz18 小时前
【C++进阶系列】:万字详解智能指针(附模拟实现的源码)
java·linux·c语言·开发语言·数据结构·c++·python
敲代码的嘎仔18 小时前
JavaWeb零基础学习Day2——JS & Vue
java·开发语言·前端·javascript·数据结构·学习·算法
yacolex18 小时前
3.3_数据结构和算法复习-栈
数据结构·算法
cookqq20 小时前
MongoDB源码delete分析oplog:从删除链路到核心函数实现
数据结构·数据库·sql·mongodb·nosql