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()
相关推荐
代码探秘者5 分钟前
【算法】吃透18种Java 算法快速读写模板
数据结构·数据库·python·算法·spring
tobebetter952714 分钟前
WSL2 + Windows + remote Chrome CDP openclaw 浏览器自动化
chrome·windows·自动化
WolfGang00732136 分钟前
代码随想录算法训练营 Day21 | 回溯算法 part03
数据结构·算法
深邃-39 分钟前
数据结构-队列
c语言·数据结构·c++·算法·html5
桌面运维家1 小时前
解决Windows 10打印机脱机:端口、驱动、网络故障排除
windows·stm32·单片机
keep intensify1 小时前
二叉树的直径
数据结构·算法·深度优先
keep intensify1 小时前
单源最短路径
数据结构·c++·算法
篮l球场1 小时前
数组中的第K个最大元素
数据结构·算法·leetcode
月落归舟2 小时前
排序算法---(三)
数据结构·算法·排序算法
liwulin05062 小时前
【ROS2】【ESP32S3纯透传方案】ESP32S3+WINDOWS+VMware+ROS2+YDLIDA X导航完整落地方案
windows·stm32·单片机