代码输出不正确

时间:2017-09-01 13:39:49

标签: python excel python-2.7 python-3.x

下面显示的代码是输出CSV表格中的所有内容。这并不是假设。相反,目标是匹配Yeardata_location,最终打印该行中的信息。

 Example if I input:
 Year=1
 data_location=1

-

Desired OUTPUT should be: Bus #:126688 Area Station:B New Load:125
                          Bus #:126695 Area Station:m New Load:85
                          Bus #:126696 Area Station:O New Load:77

-

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



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

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 #: ")

Year=year
data_location=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))

    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))



    else:
        exit

os.remove(LOAD_GEN_DATAFILE)

1 个答案:

答案 0 :(得分:0)

您的上一个if语句将始终返回true,因为您正在检查Year而不是year。至于我可以告诉你你想要做什么,你也会想要将该语句提升一级,以便在添加所有行之后执行。

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))

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))



else:
    exit

os.remove(LOAD_GEN_DATAFILE)

此外,调查dict's setdefault method会大大简化您的for循环。你可以这样做:

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]

    value = (busnum, busname, scaled_power)
    mydict.setdefault(Year, {}).setdefault(data_location, []).append(value)