1

I am receiving an error and don't know how to fix it. The error is show below along with the program. I am a beginner to programming so everything is new to me. My program's goal is to read information based of the year and location and print out the corresponding information into PSSE

Traceback (most recent call last):
File "C:/Users/RoszkowskiM/Desktop/dictreader.py", line 75, in <module>
for row in reader:
File "C:\Python27\lib\csv.py", line 103, in next
self.fieldnames
File "C:\Python27\lib\csv.py", line 90, in fieldnames
self._fieldnames = self.reader.next()
ValueError: I/O operation on closed file

_

import csv
LOAD_GEN_DATAFILE = 'C:\Users\RoszkowskiM\Desktop\Data_2017.csv'
with open(LOAD_GEN_DATAFILE) as csvfile:
reader = csv.DictReader(csvfile)
mydict = {}
for row in reader:
    Year=row['Year']
    Busnum=row['Busnum']
    Area=row['Area']
    Power=row['Power']
    TLA=row['TLA']
    Location=row['Location']
    Yearlink=row['Yearlink']
    From=row['From']
    To=row['To']
    Max=row['Max']
    Min=row['Min']

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

for 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 The list above show the TLA Pockets as well as the ID numbers assigned to them ()\n\n Please enter the ID #: ")
print("\n")


if year in Year and location in Location:  
   mydict=(Busnum,Area,Power)

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,Area,Power in mydict:
    Power= float(scaled_power)
    Busnum = int(busnum)
    print('Bus #: %d\t' % Busnum ,'Area Station: %s\t'% Area,'New Load Total: %d MW\t' % Power)


    #API for changing and scaling generation of loads
    #psspy.bsys(1,0,[0.0,0.0],0,[],1,[Busnum],0,[],0,[])
    #psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    #psspy.scal_2(0,1,2,[0,1,0,1,0],[Power,0.0,0,-.0,0.0,-.0,0])

    #print("\n")

else: exit

anatol
  • 791
  • 9
  • 16
Mark
  • 57
  • 1
  • 8
  • 5
    correct your indentation. It is really confusing – Arpit Solanki Aug 14 '17 at 15:41
  • 2
    Indentation appears to be the issue. csvfile is only open in the with statement code block. Indent all after the with statement and it should work. – Dan Aug 14 '17 at 15:43
  • fixed that. Also the code is not outputting anything. Is there something wrong with my if statement? – Mark Aug 14 '17 at 15:55
  • If you need help fixing the indentation, please take a look at [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) – PM 2Ring Aug 14 '17 at 15:57

1 Answers1

1

when you used with the file only stays open as long as your in scope, when for row in reader: is indented incorrectly your file gets closed, i tested below and get to please select year of study:

import csv
LOAD_GEN_DATAFILE = 'C:\\Users\\Dirty-Santa\\Desktop\\blah.csv'
with open(LOAD_GEN_DATAFILE) as csvfile:
    reader = csv.DictReader(csvfile)
    mydict = {}
    for row in reader:
        Year=row['Year']
        Busnum=row['Busnum']
        Area=row['Area']
        Power=row['Power']
        TLA=row['TLA']
        Location=row['Location']
        Yearlink=row['Yearlink']
        From=row['From']
        To=row['To']
        Max=row['Max']
        Min=row['Min']

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

for 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 The list above show the TLA Pockets as well as the ID numbers assigned to them ()\n\n Please enter the ID #: ")
print("\n")


if year in Year and location in Location:
   mydict=(Busnum,Area,Power)

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,Area,Power in mydict:
    Power= float(scaled_power)
    Busnum = int(busnum)
    print('Bus #: %d\t' % Busnum ,'Area Station: %s\t'% Area,'New Load Total: %d MW\t' % Power)


    #API for changing and scaling generation of loads
    #psspy.bsys(1,0,[0.0,0.0],0,[],1,[Busnum],0,[],0,[])
    #psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    #psspy.scal_2(0,1,2,[0,1,0,1,0],[Power,0.0,0,-.0,0.0,-.0,0])

    #print("\n")
BlooB
  • 955
  • 10
  • 23