【问题标题】:How to fix EOF error when reading user input in python 3?在 python 3 中读取用户输入时如何修复 EOF 错误?
【发布时间】:2017-06-17 16:31:02
【问题描述】:

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

已编辑

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

【问题讨论】:

  • 请给我们一个完整的代码 sn-p (大部分)运行并告诉我们产生错误的输入、期望的输出、实际输出以及错误的完整回溯。见How to create a Minimal, Complete, and Verifiable example
  • @RoryDaulton 我添加了更多代码 sn-p
  • 您的代码不完整,因为您有一个elif ... 行,不显示parse_one_line() 的定义,等等。您还没有显示输入、期望和实际输出以及回溯。

标签: python input eof


【解决方案1】:

问题可能出在这一行

elif: command == '..'

冒号位置不对,改成

elif command == '..':

【讨论】:

  • 那是伪代码,我运行时确实使用了正确的语法
猜你喜欢
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多