2017-09-01 10 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を返します。あなたが何をしようとしているのかを知る限り、そのステートメントを1つ上に移動して、すべての行が追加された後に実行することもできます。

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

実際には何も出力していません –

関連する問題