简体   繁体   中英

Issue with handling the reader object in python csv module

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()

csv.reader reads lines from a file object and splits them into a rows. When you hit the end of file, StopIteration is raised, the for loop catches that exception and the loop stops. Now the file pointer is at the end of the file. If you try to iterate through it a second time, its already at the end and raises StopIteration immediately. Notice in the example that nothing is printed the second time through the loop

>>> import csv
>>> fname=open('a.csv')
>>> reader = csv.reader(f)
>>> for row in reader:
...     print(row)
... 
['1', '2', '3']
['4', '5', '6']
['7', '8', '9']

>>> for row in reader:
...     print(row)
... 
>>>

One solution is to just rewind the file pointer to the start of the file. Now, the loop works again

>>> fname.seek(0,0)
0
>>> for row in reader:
...     print(row)
... 
['1', '2', '3']
['4', '5', '6']
['7', '8', '9']

Another more commonly used solution is to open the file just before the iteration. By using a while the file is closed immediately after use and the next time the loop is run, the file is opened and iterated again.

>>> with open('a.csv') as fname:
...     for row in csv.reader(fname):
...         print(row)
... 
['1', '2', '3']
['4', '5', '6']
['7', '8', '9']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM