值错误:从CSV Sh解包值

2024-06-08 17:18:50 发布

您现在位置:Python中文网/ 问答频道 /正文

这段代码的目标是链接year_linktla_2以及输出变量from_, to, digit, name2, min_value, max_value。CSV文件是读取数据的地方。它发布在这里:Excel Sheet不幸的是,我收到了这个错误:

Traceback (most recent call last):
File "C:/Users/RM/Desktop/win6.py", line 150, in <module>
for from_,to,digit,name2,min_value,max_value in voltage_envelopes[tla_2]:
ValueError: need more than 3 values to unpack.

-

from __future__ import print_function
import os.path
import win32com.client
import pdb


#---------------------------------------------------------------------------------------------------------------------
xlApp = win32com.client.DispatchEx('Excel.Application') # Running Excel
xlsPath = os.path.expanduser('C:\Users\RoszkowskiM\Desktop\UPDATED_LOAD_FORECAST_4.xlsm')# Reading xlsm file
wb = xlApp.Workbooks.Open(Filename=xlsPath) # Opening file
xlApp.Run('csvfile2')# Running macro---- csvfile2 is the macro name. It is under the "csv" module in the VBA editor
wb.Save()
xlApp.Quit()


import csv

LOAD_GEN_DATAFILE = 'C:\Users\RoszkowskiM\Desktop\Data_2017.csv' # CSV File to Read

# read the entire CSV into Python.
# CSV has columns starting with Year,busnum,busname,scaled_power,tla,location
#-------------------------------------------------------------------------------
# read the entire CSV into Python.
# CSV has columns starting with Year,busnum,busname,scaled_power,tla,location
data = list(csv.reader(open(LOAD_GEN_DATAFILE)))
mydict = {}
for row in data:
    Year,busnum,busname,scaled_power,tla,data_location,empty,year_link,from_,to,digit,name2,tla_2,min_value,max_value,last_bus = row[0:16]


    #If this is a year not seen before, add it to the dictionary
    if Year not in mydict:
        mydict[Year] = {}

    busses_in_year = mydict[Year]
    if data_location not in busses_in_year:
         busses_in_year[data_location] = []


    #Add the bus to the list of busses that stop at this location
    busses_in_year[data_location].append((busnum,busname,scaled_power))
#-------------------------------------------------------------------------------



#-------------------------------------------------------------------------------------------------------------------------------------
#User Input Statement

year = raw_input("Please Select Year of Study: ")

print("\n")

commands = ["Millwood-Buchanan", "Astoria-East-Corona", "Bronx", "DUNWOODIE-North-Sherman_Creek",
            "Vernon", "Greenwood-StatenIsland","West_49th","East_13th","Staten_Island","East_River",
            "East_View","DUNWOODIE-SOUTH","Corona-Jamaica","Astoria-East-Corona-Jamaica",
            "Astoria-West-Queensbridge-Vernon","Astoria-West-Queensbridge"]
max_columns = 50

for index, commands in enumerate(commands):
    stars_amount = max(max_columns - len(commands), 0)
    row = "# {} {}({})".format(commands, "." * stars_amount, index + 1)
    print(row)
location=raw_input(" \n The list above show the TLA Pockets as well as the ID numbers assigned to them ()\n\n Please enter the ID #: ")
#psspy.prompt_output(islct = 6)
print("\n")
Year=year_link=year
data_location=tla_2=location
#---------------------------------------------------------------------------------------------------------------------------------------

#-------------------------------------------------LOAD FORECASTS--------------------------------------------------------------------
if Year in mydict and data_location in mydict[Year]:  
    busses_in_year = mydict[Year]
    #print("Here are all the busses at that location for that year and the new LOAD TOTAL: ")
    #print("\n")

    #Busnum, busname,scaled_power read from excel sheet matching year and location

    for busnum,busname,scaled_power in busses_in_year[data_location]:
        scaled_power= float(scaled_power)
        busnum = int(busnum)
        output='Bus #: {}\t Area Station: {}\t New Load Total: {} MW\t'
        print(output.format(busnum,busname,scaled_power))
        #psspy.bsys(1,0,[0.0,0.0],0,[],1,[busnum],0,[],0,[])
        #psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
        #psspy.scal_2(0,1,2,[0,1,0,1,0],[scaled_power,0.0,0,-.0,0.0,-.0,0])


else:
    exit

data = list(csv.reader(open(LOAD_GEN_DATAFILE)))
year = raw_input(" ")
location=raw_input(" ")
for row in data:
    year_link, from_,to,digit,name2,tla_2,min_value,max_value,last_bus = row[7:16]
    year_link = year
    tla_2=location
    if year_link not in mydict:
        mydict[year_link]={}

    voltage_envelopes=mydict[year_link]

    if tla_2 not in voltage_envelopes:
        voltage_envelopes[tla_2]=[]

    voltage_envelopes[tla_2].append((from_,to,digit,name2,min_value,max_value))

if year_link in mydict and tla_2 in mydict[year_link]:
     voltage_envelopes=mydict[year_link]

     for from_,to,digit,name2,min_value,max_value in voltage_envelopes[tla_2]:
         from_=int(from_)
         to=int(to)
         min_value=float(min_value)
         max_value=float(max_value)
         digit=int(digit)

         output = 'From Bus #: {}\tTo Bus #: {}\t Area Station: {}\t VMIN: {} pu\tVMAX: {} pu\t' 
         print(output.format(from_, to,name2, min_value, max_value))
         print("\n")

        #_c=psspy.getdefaultchar()
        #_i=psspy.getdefaultint()
        #_f=psspy.getdefaultreal()
        #psspy.two_winding_chng_4(from_,to,'%d'% digit,[_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i,_i],[_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f,_f, max_value, min_value,_f,_f,_f],[])
else:
    exit

Tags: thetoinfromdatavaluelinklocation