本文件包括fluent导出文件保存到excel中
将多个excel中的文件合并到一个excel中的多个sheet中
读取一个excel中的多个sheet文件名
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 21 20:09:01 2020
@author:
"""
import pandas as pd
import re
import os
import numpy as np
import sys
sys.setrecursionlimit(50000)
print('!!!!!!!!!*** 注意 ***!!!!!!!!!!')
print('''1、文件名命名例子:1-xv20.00,2-yv20.00,3-zs20.00....
2、命名需要严格按 1 给出的实例,否则结果不准确,但可通过修改代码 计算 部分标注的位置来完成修正
3、除1-xv20.00外的所有文件,处理时只保存其第二列''')
'''删除序列中相同的元素,并且保持顺序'''
def delsame(items):
xin = []
for qq in items:
# 如果不在列表中
if qq not in xin:
xin.append(qq)
return xin
'''创建文件夹'''
def mkdir(path):
# 去除首位空格
path = path.strip()
# 去除尾部 \ 符号
path = path.rstrip("\\")
# 判断路径是否存在
isExists = os.path.exists(path)
# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
print(path + ' 创建成功')
return path
else:
# 如果目录存在则不创建,并提示目录已存在
print(path + ' 目录已存在')
return path
def pjs():
path1 = input('请输入文件路径:')
path_flie1 = list(os.walk(path1))[0][2]#获取当前文件夹下的文件
# print(dirpath)
# print(dirnames)
num = 0
ycl = mkdir(path1+'\\' +'0-pretreatment')
print('''---------------------------------------------------------------
开始预处理''')
for name in path_flie1:
num += 1
print('------正在处理%s------'%(name))
pf = path1 +'\\' + name
data = pd.read_csv(pf)
data_sep = data.iloc[:,0].tolist()
data_cl = []
data_x = []
data_y = []
#处理数据
for k in data_sep:
data_k = re.split(r'[\(\t\)\ \" ]',k)
[data_k.remove(i) for i in data_k if i == '']
data_cll=[x.strip() for x in data_k if x.strip() != '']
if 'labels' not in k:
data_cl.append(data_cll)
while [] in data_cl:
data_cl.remove([])
for s in data_cl:
data_x.append(s[0])
data_y.append(s[1])
#整理输出到文件
data_x_index = [i for i,x in enumerate(data_x) if x == data_x[0] ]#获取下标
for x in range(len(data_x_index)):
if eval(name[0]) == 1:
if x < len(data_x_index)-1:
dataframe = pd.DataFrame({'x':data_x[data_x_index[x]:data_x_index[x+1]],'液含率':data_y[data_x_index[x]:data_x_index[x+1]]})
dataframe.to_excel(ycl + '\\' + name[0] +'-'+name +'-'+data_y[data_x_index[x]]+'.xlsx',encoding='gb2312',index =False)
else:
dataframe = pd.DataFrame({'x':data_x[data_x_index[x]:],'液含率':data_y[data_x_index[x]:]})
dataframe.to_excel(ycl + '\\' + name[0]+'-' +name +'-'+data_y[data_x_index[x]]+'.xlsx',encoding='gb2312',index =False)
else:
if x < len(data_x_index)-1:
dataframe = pd.DataFrame({'velocity':data_y[data_x_index[x]:data_x_index[x+1]]})
dataframe.to_excel(ycl + '\\' + name[0] +'-'+name +'-'+data_y[data_x_index[x]]+'.xlsx',encoding='gb2312',index =False)
else:
dataframe = pd.DataFrame({'velocity':data_y[data_x_index[x]:]})
dataframe.to_excel(ycl + '\\' + name[0]+'-' +name +'-'+data_y[data_x_index[x]]+'.xlsx',encoding='gb2312',index =False)
print(''' 预处理完成''')
print('文件保存在--%s\n\
---------------------------------------------------------------'%(ycl))
'''合并'''
print(''' 开始合并''')
path_flie2 = list(os.walk(ycl))[0][2]#获取 0-预处理 文件夹下的文件
path_new = mkdir(path1+'\\' +'1-merge')
heb = []#用来保存预处理文件夹下的文件名
for txt_item in path_flie2:
heb.append(txt_item)
time_jiemian = [po[6:] for po in heb]
time_jiemian_bef = [po[:6] for po in heb]
mer_file_name_behind = delsame(time_jiemian)
mer_file_name_before = delsame(time_jiemian_bef)
print('''*****************************************
检查 前缀名顺序 是否正确,检查 后缀名 是否正确,两者可以连接成 0-预处理 文件夹下的名称
*****************************************''')
print('文件前缀名',mer_file_name_before)
print('文件后缀名',mer_file_name_behind[0:2])
for ii in mer_file_name_behind:
merge_name_file = []
for l in mer_file_name_before:
test_name = pd.read_excel(ycl+'\\'+l+ii,encoding='gb2312')
merge_name_file.append(test_name)
df = pd.concat(merge_name_file,axis=1)
print('------正在合并%s------'%(ii))
df.to_excel(path_new+'\\'+'merge-'+ii,index=False,encoding='gb2312')
print(''' 合并完成''')
print('文件保存在--%s\n\
---------------------------------------------------------------'%(path_new))
'''汇总信息'''
print('''----------------------------------------
开始删除-合并''')
path_flie_merge = list(os.walk(path_new))[0][2]#获取 1-合并 文件夹下的文件名
path_new1 = mkdir(path1+'\\' +'2-collect')#创建 2-删除 文件
mer_sheet_behind = [po[-7:] for po in path_flie_merge]
mer_sheet_before = [po[:-7] for po in path_flie_merge]
sheet_behind = delsame(mer_sheet_behind)
sheet_before = delsame(mer_sheet_before)
for shnum in sheet_before:
print('------正在删除-合并%s------'%(shnum))
writer = pd.ExcelWriter(path_new1+'\\'+'col-'+shnum[6:-1]+'.xlsx')
for sh in sheet_behind:
test_name = pd.read_excel(path_new+'\\'+shnum+sh,encoding='gb2312')
test_del = test_name.drop([0])#删除第0行
test_del.to_excel(writer, sheet_name=sh[0:2],index = False)
writer.save()
writer.close()
print(''' 删除-合并完成''')
print('文件保存在--%s\n\
---------------------------------------------------------------'%(path_new1))
'''计算'''
print('''----------------------------------------
开始计算''')
path_flie_del = list(os.walk(path_new1))[0][2]#获取 2-collect 文件夹下的文件
path_new_result = mkdir(path1+'\\' +'3-result')
drop_lv= []
drop_velo = []
res_writer_mean = pd.ExcelWriter(path_new_result+'\\'+'mean'+'.xlsx')
for js in path_flie_del:
drop_pos = []
drop_lv= []
drop_velo = []
res_writer = pd.ExcelWriter(path_new_result+'\\'+'result-'+js[3:]+'.xlsx')
shuru = eval(input('请输入%s对应的值:'%(js)))
print('------正在计算%s------'%(js))
result_name = pd.read_excel(path_new1+'\\'+js,sheet_name=None,encoding='gb2312')#获取excel种的不同sheet,读取必须加sheet_name=None
for jn in list(result_name.keys()):
result_sheet = pd.read_excel(path_new1+'\\'+js,sheet_name=jn,encoding='gb2312')
'''更改的位置'''
d1 = result_sheet.loc[(result_sheet["x"] <0.095) & (result_sheet["x"]>0.0045)]['x']#根据条件筛选,取x列
d2 = result_sheet.loc[(result_sheet["x"] <0.095) & (result_sheet["x"]>0.0045)]['液含率']
d3 = result_sheet.loc[(result_sheet["x"] <0.095) & (result_sheet["x"]>0.0045)]['velocity']
result_sheet['液含率'] = 1-d2
result_sheet['液体真实速度'] = shuru/(1-d2)
result_sheet['液固相对速度'] = abs(shuru/(1-d2)-d3)
'''结束的位置'''
dataover = result_sheet.loc[(result_sheet["x"] <0.095) & (result_sheet["x"]>0.0045)]#取x列该范围内的数值保存
'''更改的位置'''
drop_pos.append(jn)
drop_lv.append(dataover['液含率'].mean())#保存平均值
drop_velo.append(dataover['液固相对速度'].mean())#保存平均值
'''结束的位置'''
dataover.to_excel(res_writer, sheet_name=jn,index = False)
drop_pos.append('总平均')
drop_lv.append(np.mean(drop_lv))
drop_velo.append(np.mean(drop_velo))
outpot_mean = pd.DataFrame({'位置':drop_pos,'液含率平均值':drop_lv,'液固相对速度平均值':drop_velo} )
outpot_mean.to_excel(res_writer_mean, sheet_name=js[4:9],index = False)
res_writer.save()
res_writer.close()
res_writer_mean.save()
res_writer_mean.close()
print(''' 计算完成''')
print('文件保存在--%s\n\
---------------------------------------------------------------'%(path_new_result))
zd = 1
while zd:
pjs()
zd = eval(input('是否继续计算,若是请输入任意数字,退出请输入0:'))