【发布时间】:2020-01-03 08:13:53
【问题描述】:
投票已经开始了……由您来确保宣布正确的获胜者!
您获得了一个名为 nominees.csv 的 CSV 文件,其中包含获得奖项提名的各种电影的名称,以及应宣布为获奖者的人。该文件将如下所示:
title,director(s)
Schindler's List,Steven Spielberg
"O Brother, Where Art Thou?","Joel Coen, Ethan Coen"
2001: A Space Odyssey,Stanley Kubrick
"Sherlock, Jr.","Buster Keaton, Roscoe Arbuckle"
您应该编写一个读取 nominees.csv 的程序,询问获奖作品的名称,并打印出具体的祝贺信息。例如,使用上面的文件,你的程序应该像这样工作:
Winning title: O Brother, Where Art Thou?
Congratulations: Joel Coen, Ethan Coen
这是另一个例子,使用相同的文件:
Winning title: Schindler's List
Congratulations: Steven Spielberg
已经尝试提交和更改值,但第 10 行总是给出值错误,第 15 行也是如此。当应用新的被提名者列表时,它会给出错误并使我的代码失败。
def main():
film_director=[]
with open('nominees.csv','r') as read_file:
lines=read_file.readlines()
lines=lines[1:]
for line in lines:
if '"' in line:
if line[0]=='"':
index_second_quotes=line.index('"',1)
index_third_quotes=line.index('"',index_second_quotes+1)
title = line[:index_second_quotes].strip('\"')
directors=line[index_third_quotes:-1].strip('\"').strip()
else:
index_first_quotes = line.index('"')
index_second_quotes = line.index('"', index_first_quotes+1)
title = line[:index_first_quotes-1].strip('\"')
directors = line[index_first_quotes+1:-1].strip('\"').strip()
film_director.append([title,directors])
else:
tokens = line.split(',')
film_director.append([tokens[0].strip(),tokens[1].strip()])
title = input('Winning title: ')
for row in film_director:
if title.strip()==row[0]:
print('Congratulations:',row[1])
break
main()
给出的错误信息是:
Testing a new nominees file. Your submission raised an exception of type ValueError. This occurred on line 10 of program.py.
【问题讨论】:
-
这是作业题吗?
-
不是作业问题。提高 Python 技能,但在过去 3 小时内一直坚持这一技能。将aprpreciate帮助或解决方案。谢谢
-
顺便说一句,您的代码工作正常:repl.it/repls/FairBraveLaw
-
这是一个在线测试网站吗?它应该在 Python 2 还是 3 上运行?
-
假设在 python 3 上运行
标签: python csv exception valueerror