The goal I am trying to accomplish is reading in only the particular data I want from a large csv file. To do this, I have a main menu that I use as a handler for data acquisition and then a separate menu for exiting or continuing. My issue arises when I attempt to read in more data after looping through the file once. The issue being that I have reached the end of the file and for some reason the for loop is not handling the StopIterator error correctly. Any suggestions? Thanks in advance!
fname = open(current_file, 'r')
reader = csv.reader(fname)
for row in reader:
list_header = row
break
def main_menu():
i=0
menu = {}
print reader
for hdr in list_header:
menu[str(i)]=hdr
i+=1;
options=menu.keys() #creates a list out of the keys
options.sort()
for entry in options:
print entry, menu[entry]
selection=raw_input("Please Select:")
data=[]
for row in reader:
a=0
for block in row:
if a==list_header.index(menu[selection]):
data.append(block)
a+=1
print 'Saving '+menu[selection]+' values into an array.'+'\n'
return data
def continue_menu():
menu_1={}
menu_1['0']='Continue'
menu_1['1']='Exit'
options=menu_1.keys()
options.sort()
for entry in options:
print entry, menu_1[entry]
selection=raw_input('Please Select:')
if float(selection)==0:
print 'As you wish'+'\n'
proceed=True
else:
proceed=False
return proceed
proceed=True
while proceed:
data1=main_menu()
proceed=continue_menu()
main_menutwice? If so,for row in readerhas no data to read the second time around and its code block is skipped and an empty list is returned. If you don't give us more details about the problem there is no way for us to help.continue_menudoesn't touch the reader or its underlying file object so from an iteration point of view, it makes no difference. Once you've gone through theforloop, thefanmefile pointer is at the end of the file. You need to reset that pointer. I'll put an example as an aswer.