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()
相关推荐
NineData1 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库
阿白的白日梦4 天前
winget基础管理---更新/修改源为国内源
windows
埃博拉酱8 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21889 天前
.NET 本地Db数据库-技术方案选型
windows·c#
加号39 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
琢磨先生David9 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
tryCbest9 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅9 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技9 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~9 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑