1

我似乎将自己与循环混淆,无法修复,我希望生成一个数据集,该数据集在我的 excel 表中向右和向下移动,但由于某种原因,每个新行都会在第一行的末尾被截断,因此我的回溯错误,试图读取上面的空单元格。我希望能有一些新鲜的眼光,因为我后来的代码依赖于这个正确的输出,希望它能够解决全球经济衰退,或者不是,但会让我成为一个快乐的人。:)

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'
4

2 回答 2

2

I totally don't get what are you trying to compute, but here's the cause of the mentioned exception.

First of all, in your first for loop, in the excel file you take a row (row #1), and fill the columns in that row from column #1 to column #52 (indexing starts from 0):

Later, in the second for loop, when you read the values, you increase the global counter so when the exception is thrown, you are trying to read the values for the 53th week.

Solution: instead of

        ECDD1 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
        ECDD2 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
        EC_demand = ECDD1 - (ECDD2*FC_Error)

you could

        ECDD1 = sheet1.cell(row=Prev_Week, column=DiffWeek).value
        ECDD2 = sheet1.cell(row=Prev_Week, column=DiffWeek).value
        EC_demand = ECDD1 - (ECDD2*FC_Error)

You actually created the DiffWeek variable but forgot to use it.

于 2012-08-24T09:27:18.770 回答
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')
于 2012-08-24T10:51:56.530 回答