如何解决读取python 3中的用户输入时的EOF错误?

问题描述:

我需要从用户输入读取多行,将它们解析为命令和调用函数。即使在我抛出异常之后,我仍然不断收到EOFError。如果我在'try'中放入if..else语句,也会发生同样的事情。程序在主程序停止,不会调用函数。如何解决读取python 3中的用户输入时的EOF错误?

EDITED

infile = open('file.csv') 
weather = list() 
for line in infile: 
    parse_one_line() #parse each row into tuples 
        #and add them into a list 

while True: 
    try: 
     input_stream = input() 
     command = input_stream.split()   
    except ValueError: 
     pass 

    if command == []: 
     pass 
    elif command[:4] == ['filter', 'TEMP', 'at', 'least']: 
     filterRecord() #user input "filter TEMP at least <integer>" 
    elif ... 

def filterRecord(): #filter rows that meet 
        #the criteria into a new list 
    global filtered 
    filtered = list() 
    try: 
     for x in range(len(weather)): 
      if int(weather[x][2]) >= int(command[-1]): 
       print(weather[x]) 
       filtered.append(tuple(weather[x])) 
    except ValueError: 
     pass 
+1

请给我们一个完整的代码片段(主要)运行并告诉我们输入会产生错误,这个愿望d输出,实际输出和错误的完整回溯。请参见[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –

+0

@RoryDaulton我添加了更多的代码片段 –

+0

由于您有'elif ...'行,因此您的代码不完整,不会显示'parse_one_line()'的定义,依此类推。您还没有显示输入,期望和实际输出以及回溯。 –

这个问题可能是这一行

elif: command == '..' 

冒号在错误的地方,将其更改为

elif command == '..': 
+0

这是伪代码,我运行它时使用了正确的语法 –