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()
相关推荐
Lazionr26 分钟前
模板进阶:深入理解模板机制
c++·windows
豆沙沙包?7 小时前
C++~~~stack容器、queue容器、list容器(p45-P56)
c++·windows·list
LuminousCPP12 小时前
数据结构‑二叉树(二):二叉堆从零实现|Heap结构设计 + 建堆优化 + 堆排序深度解析
c语言·数据结构·笔记·二叉树
13 小时前
数据结构第一课:复杂度解析
数据结构
戒了,最后一次14 小时前
DeepSeek Harness 源码安装教程(Windows 篇)
windows·腾讯云·deepseek·harness
欧叶冲冲冲15 小时前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
_Narcissus_16 小时前
链表算法题和静态链表
数据结构·c++·笔记·算法·链表·ai·力扣
Lyyaoo.16 小时前
【普通数组】【中等】除了自身以外数组的乘积
数据结构·算法·leetcode
Flag在我手里16 小时前
应急响应-Windows权限维持
windows·rdp·应急响应·玄机
疯狂打码的少年17 小时前
【数据结构】八大排序算法对比总结(时间/空间/稳定性)
数据结构·笔记·算法