我从循环创建一个文件夹如何保存excel表
path = os.path.dirname(('E:/python/Results/'))
for No_of_system in range(1,3):
TaskAttribute = char.Task_Att(1)
fle = os.mkdir(path + "\" + str(No_of_system))
w = pd.ExcelWriter('fle/withoutheuristic.xlsx')
w1 = pd.ExcelWriter('fle/withheuristic.xlsx')
for No_of_run in range(0,2):
#RANDOMLY CREATING HOSTS
for i in range(cf.Config.get_population_size()):
chromo = (createindivisual.init_solution())
Obj = Objective(i, chromo, TaskAttribute)
d = {'Iteration': with_out_heuristic(Obj)[0], 'Best_fitness':
with_out_heuristic(Obj)[1], 'No.of_Task':
cf.Config.get_Task(), 'No.of_system': No_of_system}
df = pd.DataFrame(data=d)
print(df)
d1 = {'Iteration': with_heuristic(Obj)[0], 'Best_fitness':
with_heuristic(Obj)[1], 'No.of_Task': cf.Config.get_Task()}
df1 = pd.DataFrame(data=d1)
print(df1)
df.to_excel(w,'sheet%s' % No_of_run, index=False)
df1.to_excel(w1,'sheet%s' % No_of_run, index=False)
w.save()
w1.save()
这是我的代码,但无法将excel表保存在创建文件夹中,任何人都可以帮助我plz
您的代码有点乱,所以我在这里做一个更简单的例子。
import os
import pandas as pd
for path in ["dir1", "dir2", "dir3"]:
os.mkdir(path)
df.to_excel(path + "/filename.xlsx", sheet_name="sheet1")
因此,本质上,对于您的代码,请尝试添加路径
w = pd.ExcelWriter(path + "/" + str(No_of_system) '/fle/withoutheuristic.xlsx')
w1 = pd.ExcelWriter(path + "/" + str(No_of_system) '/fle/withheuristic.xlsx')
或类似的