Python Forum
Code is Outputting Incorrectly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code is Outputting Incorrectly
#1
The code shown below is outputting everything on my CSV sheet. It's not suppose to do that. Instead, the goal is to match Year and data_location, which finally prints the information in that row. So for example if I input:
Year=1
data_location=1
Desired output should be:
Output:
Busnum:126688 busname:B New Load:125
Output:
Busnum:126695 busname:m New Load:85
Output:
Busnum:126696 busname: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)
        
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading a directory and outputting as sound loungeroom 0 1,234 Jul-30-2021, 09:26 PM
Last Post: loungeroom
  Writing to file ends incorrectly project_science 4 2,708 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Opening file and outputting its text content example leodavinci1990 1 1,892 Oct-12-2020, 05:33 AM
Last Post: buran
  Outputting Sorted Text files Help charlieroberrts 1 1,748 Jul-05-2020, 08:37 PM
Last Post: menator01
  New to the language. What am I doing incorrectly here? christopher3786 3 2,267 Jun-20-2020, 10:18 AM
Last Post: pyzyx3qwerty
  my openpyxl use is too slow, am I reading rows incorrectly? Clunk_Head 2 8,345 Apr-30-2020, 10:29 PM
Last Post: deac33
  Outputting results into other program ancient650 1 1,866 Jun-11-2019, 02:49 PM
Last Post: gontajones
  list is printing incorrectly.. anna 1 2,100 May-18-2019, 12:12 PM
Last Post: ichabod801
  Python only outputting "101" Spuffle 4 9,282 Jan-09-2019, 10:07 AM
Last Post: buran
  Outputting a float value in a print() statement RedSkeleton007 2 3,536 Jan-11-2018, 09:23 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020