python - Error: Too many values to unpack with CSV File -
my program shown below has goal of outputting statements based off 2 criteria:(year
, location
) once accomplished, next goal match year
yearlink
, location
location
. location
, year
both input statements. output should statement outputs values row. reason, receive error many values unpack. excel sheet posted if wants better idea of talking about. anyway tried nothing working me.
traceback (most recent call last): file "c:/users/roszkowskim/desktop/win4.py", line 134, in <module> from,to,max,min in data: valueerror: many values unpack load_gen_datafile = 'c:\users\roszkowskim\desktop\data_2016.csv' # csv file read # read entire csv python. # csv has columns starting year,busnum,busname,scaled_power,tla,location data = list(csv.reader(open(load_gen_datafile))) mydict = {} row in data: year,busnum,busname,scaled_power,tla,location,yearlink,from,to,max,min = row[0:12] #if year not seen before, add dictionary if year not in mydict: mydict[year] = {} busses_in_year = mydict[year] if location not in busses_in_year: busses_in_year[location] = [] #add bus list of busses stop @ location busses_in_year[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 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 list above show tla pockets id numbers assigned them ()\n\n please enter id #: ") print("\n") year=year location=location if year in mydict , location in mydict[year]: busses_in_year = mydict[year] print("here busses @ location year , new load total: ") print("\n") #busnum, busname,scaled_power read excel sheet matching year , location busnum,busname,scaled_power in busses_in_year[location]: scaled_power= float(scaled_power) busnum = int(busnum) print('bus #: %d\t' % busnum ,'area station: %s\t'% busname,'new load total: %d mw\t' % scaled_power) else: exit row in data: year,busnum,busname,scaled_power,tla,location,yearlink,from,to,max,min = row[0:11] if yearlink==year , location==location: from,to,max,min in data: from=int(from) to=int(to) max=float(max) min=float(min) print('from bus #: %d\t' % ,'to bus #: %d\t'% to,'vmax: %d pu\t' % max, 'vmin: %d pu\t' % min) else: exit
have considered using dictreader? variable unpacking seem bit of code smell, , more subject errors, opposed loading entire row dictionary.
Comments
Post a Comment