[英]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. 我要实现的目标是仅从大型csv文件中读取所需的特定数据。 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.
问题是我已经到达文件末尾,由于某种原因,for循环无法正确处理StopIterator错误。 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. csv.reader
从文件对象读取行,并将其拆分为行。 When you hit the end of file, StopIteration
is raised, the for
loop catches that exception and the loop stops. 当您到达文件末尾时,将引发
StopIteration
, for
循环将捕获该异常,然后循环将停止。 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. 如果您尝试第二次遍历它,它已经在末尾并立即引发
StopIteration
。 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. 通过使用
while
该文件的使用和运行循环的下一次后立即关闭,该文件被打开,并再次重复。
>>> with open('a.csv') as fname:
... for row in csv.reader(fname):
... print(row)
...
['1', '2', '3']
['4', '5', '6']
['7', '8', '9']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.