【发布时间】: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()的定义,等等。您还没有显示输入、期望和实际输出以及回溯。