python提取多个文件汇总到一个excel表

背景介绍

perl拆分大文件后我们得到下图信息

windsim处理结果

我们希望单独提取三列信息到一个excel表大文件夹内,方便比较数据

适合任何风电场!

技术分解

  1. 遍历
  2. 提却对应文件标识信息,并保存计算结果到xlsx文件

技术实现

主要用到pandas的ExcelWriter函数

import numpy as np
import pandas as pd
import os


#headDir=r"C:\Users\yezhaoliang\Desktop\work\AIWind\CompareCloseTOSingle\8.00"
headDir=r"C:\Users\yezhaoliang\Desktop\work\AIWind\processWindSim\HuaiLaiTurbine"
#headDir=r"D:\BoundaryLines"


def get_files(path=headDir, rule=".csv"):
    all = []
    for fpathe,dirs,fs in os.walk(path):   # os.walk是获取所有的目录
        for f in fs:
            filename = os.path.join(fpathe,f)
            if filename.endswith(rule):  # 判断是否是"xxx"结尾
                all.append(filename)
    return all

if __name__ == "__main__":
    b = get_files()
    writer=pd.ExcelWriter("Results-WindSimHuaiLaiTurbine.xlsx")
    altitudesName=[]
    # googleEarthAltitude=[]
    # computedAltitude=[]
    # deltaAltitude=[]
    for i in b:
        them=i.split("\\")
        #secondPart=them[them.__len__()-2]
        # sheetName=them[them.__len__()-3]+them[them.__len__()-2]+them[them.__len__()-1]
        #sheetName=them[them.__len__()-3]+them[them.__len__()-2]+them[them.__len__()-1]
        sheetName=them[them.__len__()-1]
        altitudesName.append(sheetName)
        sd=pd.read_csv(i,skiprows=3)
        ## 去除Null行(注意空格,数字也一样处理)
        #squeezeZseries = sdNotNull.iloc[0:300, 2] - sdNotNull.iat[0, 2]  ## 减去海拔值

        result = pd.DataFrame({
            'Velocity v/(m/s)':sd.iloc[:,5],
            'KE /(m*2/s*2)':sd.iloc[:,10],
            'Z/(m)':sd.iloc[:,1]})
        result.to_excel(writer,sheet_name=sheetName)
    writer.save()
    writer.close()
Related
叶昭良
叶昭良
Engineer of offshore wind turbine technique research

My research interests include distributed energy, wind turbine power generation technique , Computational fluid dynamic and programmable matter.