我似乎把自己弄得一团糟无法修复,我期待生成一个在我的Excel工作表中向右和向下移动的数据集,但由于某种原因,每个新行都会在第一行的末尾被截断,因此我的追溯错误,whcih试图读取上面的空单元格。我会欣赏一些新鲜的眼睛,因为我后来的代码依赖于这个正确的输出,并希望它能解决全球经济衰退,或者不会,但会让我成为一个快乐的人。 :)
import datetime
import random
import numpy as np
import math
import sys
from openpyxl import Workbook
from openpyxl.cell import get_column_letter
from openpyxl import load_workbook
from tempfile import TemporaryFile
from random import normalvariate, expovariate, seed, gammavariate
from math import sqrt
from itertools import groupby
for week in CurrentWeek:
Global_counter = week
i = Global_counter
j = 52 + i
if Global_counter == 1:
for index in range(i,j,1):
PlanWeek = index
EC_D = 500.0
if EC_D <= 0:
EC_D = EC_D *(-1)
EC_PlanData.append(EC_D)
d = sheet1.cell(row = Global_counter, column = PlanWeek)
d2 = sheet2.cell(row = Global_counter, column = PlanWeek)
d.value = EC_D
d2.value = EC_D
else:
for index in range(i,j,1):
PlanWeek = index
DiffWeek = PlanWeek - Global_counter
Sigma = 1
mu = 0
FC_VZ = random.normalvariate(mu, Sigma)
if FC_VZ > 0:
FC_VZ = 1
FC_Error = FC_Error+RemainingFCError
fcerr = sheet5.cell(row = Global_counter, column = PlanWeek)
fcerr.value = FC_Error
Prev_Week = Global_counter - 1
ECDD1 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
ECDD2 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
EC_D = ECDD1 - (ECDD2*FC_Error)
FC_Error = 0
ECPD = sheet1.cell(row = Global_counter, column=PlanWeek)
ECPD.value = EC_D
FCAD = sheet6.cell(row = Global_counter, column=PlanWeek)
FCAD.value = FC_Error
IEFD = sheet2.cell(row = Global_counter, column=PlanWeek)
IEFD.value = EC_D
EC_D = 0
wb.save('RHF_Loop_test4py.xlsx')
我的追溯是:
Traceback (most recent call last):
File "C:\Python27\Darren Learning Samples\codetesting2.py", line 85, in <module>
EC_D = ECDD1 - (ECDD2*FC_Error)
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
答案 0 :(得分:2)
我完全没有得到你想要计算的东西,但这是上述异常的原因。
首先,在您的第一个for
循环中,在excel文件中取一行(第1行),并填充该行中从第1列到第52列的列(索引从0):
稍后,在第二个for
循环中,当您读取值时,会增加全局计数器,因此在抛出异常时,您将尝试读取第53周的值。
解决方案:而不是
ECDD1 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
ECDD2 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
EC_demand = ECDD1 - (ECDD2*FC_Error)
你可以
ECDD1 = sheet1.cell(row=Prev_Week, column=DiffWeek).value
ECDD2 = sheet1.cell(row=Prev_Week, column=DiffWeek).value
EC_demand = ECDD1 - (ECDD2*FC_Error)
您实际创建了DiffWeek
变量但忘记使用它。
答案 1 :(得分:0)
import datetime
import random
import numpy as np
import math
import sys
from openpyxl import Workbook
from openpyxl.cell import get_column_letter
from openpyxl import load_workbook
from tempfile import TemporaryFile
from random import normalvariate, expovariate, seed, gammavariate
from math import sqrt
from itertools import groupby
## Test Lists for testing code
lst_FC_error = []
## Using Lists and Excel
wb = Workbook()
dest_filename = r'RHF_Loop_test4py.xlsx'
EC_PlanData = []
sheet1 = wb.create_sheet(0)
sheet1.title = "EC_PlanData"
EC_FC_Data_original = []
sheet2 = wb.create_sheet(1)
sheet2.title = "EC_FC_Data_original"
FC_AccData = []
sheet5 = wb.create_sheet(4)
sheet5.title = "FC_Acc_Data"
EC_FC_Data_commited = []
sheet6 = wb.create_sheet(5)
sheet6.title = "EC_FC_Data_commited"
FC_Error = 0
alpha = 50
beta = 10
CurrentWeekRangeSize = 10
CurrentWeek = range(1,CurrentWeekRangeSize,1)
RemainingFCError = .2
for Global_counter in CurrentWeek:
if Global_counter == 1:
for index in range(Global_counter,Global_counter + 52 + CurrentWeekRangeSize,1):
PlanWeek = index
EC_demand = 500.0
if EC_demand <= 0:
EC_demand = EC_demand *(-1)
EC_PlanData.append(EC_demand)
d = sheet1.cell(row = Global_counter, column = PlanWeek)
print "hello", Global_counter, PlanWeek
d2 = sheet2.cell(row = Global_counter, column = PlanWeek)
d.value = EC_demand
d2.value = EC_demand
wb.save('RHF_Loop_test4py.xlsx')
print "-------------"
else:
#this was the original logic:
#for index in range(Global_counter,Global_counter + 52,1):
for index in range(Global_counter,52,1):
PlanWeek = index
DiffWeek = PlanWeek - Global_counter
Sigma = 1
mu = 0
FC_VZ = random.normalvariate(mu, Sigma)
if FC_VZ > 0:
FC_VZ = 1
FC_Error = FC_Error+RemainingFCError ## decrease demand
fcerr = sheet5.cell(row = Global_counter, column = PlanWeek)
fcerr.value = FC_Error
Prev_Week = Global_counter - 1
ECDD1 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
print Prev_Week, PlanWeek, ECDD1
ECDD2 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
EC_demand = ECDD1 - (ECDD2*FC_Error)
FC_Error = 0
ECPD = sheet1.cell(row = Global_counter, column=PlanWeek)
ECPD.value = EC_demand
FCAD = sheet6.cell(row = Global_counter, column=PlanWeek)
FCAD.value = FC_Error
IEFD = sheet2.cell(row = Global_counter, column=PlanWeek)
IEFD.value = EC_demand
EC_demand = 0
wb.save('RHF_Loop_test4py.xlsx')