如何在python 3中读取用户输入时修复EOF错误?

时间:2017-06-17 16:31:03

标签: python input eof

我需要从用户输入中读取多行,将它们解析为命令和调用函数。在我抛出异常后,我一直得到EOFError。如果我将if..else语句放在'try'中,也会发生同样的事情。程序在main处停止,不会调用函数。

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 个答案:

答案 0 :(得分:0)

问题可能在于这一行

elif: command == '..'

冒号位于错误的位置,将其更改为

elif command == '..':