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

何を計算しようとしているのかまったくわかりませんが、上記の例外の原因は次のとおりです。

まず、最初のforループで、Excelファイルで行(行#1)を取得し、その行の列を列#1から列#52(インデックス作成は0から開始)まで入力します。

後で、2番目の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変数を作成しましたが、使用するのを忘れました。

于 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 に答える