2017-09-01 307 views
-3

下面顯示的代碼正在輸出我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) 

回答

0

因爲你檢查Year而不是year你最後if語句總是返回true。就我可以告訴你想要做什麼而言,你還希望將該語句上移一層,以便在添加所有行後執行。

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

我不熟悉'set default'。你能舉一個例子說明如何將它應用到我的代碼中。 –

+0

這是否解決了您的問題? –

+0

其實現在沒有什麼東西在輸出 –