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()
相关推荐
代码小书生8 分钟前
Windows X-Lite Win11 26H1 v3 游戏优化系统!集Win11、Win10、Win7三代优点,兼顾游戏办公生产算力,系统精简纯净!
windows·win10·电脑系统·windows10·26h1·windows x-lite·操作系统操作系统
自我意识的多元宇宙41 分钟前
树与二叉树--二叉树的存储结构
数据结构
贵沫末2 小时前
python——打包自己的库并安装
开发语言·windows·python
小眼哥3 小时前
SpringBoot整合Vue代码生成exe运行程序以及windows安装包
vue.js·windows·spring boot
xiaoshuaishuai83 小时前
C# GPU算力与管理
开发语言·windows·c#
自我意识的多元宇宙5 小时前
二叉树的遍历和线索二叉树--二叉树的遍历
数据结构
qq_5024289905 小时前
清华大学与微软亚洲研究院出品:Kronos 本地部署教程
数据结构·python·金融量化·kronos开源模型
꯭爿꯭巎꯭6 小时前
千鹿PR助手邀请码
windows
C雨后彩虹7 小时前
最多等和不相交连续子序列
java·数据结构·算法·华为·面试