In: Computer Science
This question has been done before but when i submit i receive a type ValueError on line 9 where it says index_second_qoutes + 1. Could you please help or update code if needed. Thank U. (Code has been attached at the the end).
The votes are in… and it's up to you to make sure the correct winner is announced!
You've been given a CSV file called nominees.csv, which contains the names of various movies nominated for a prize, and the people who should be announced as the recipient. The file will look like this:
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"
You should write a program that reads in nominees.csv, asks for the name of the winning title, and prints out specific congratulations. For example, with the above file, your program should work like this:
Winning title: O Brother, Where Art Thou?
Congratulations: Joel Coen, Ethan Coen
Here is another example, using the same file:
Winning title: Schindler's List
Congratulations: Steven Spielberg
Code: 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: index_second_quotes=line.index('"',1) index_third_quotes=line.index('"',index_second_quotes+1) This line gives the type Value Error title = line[:index_second_quotes].strip('\"') directors=line[index_third_quotes:-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()
If you have any doubts, please give me comment...
It raises an error, if title doesn't have double quotes and directors have double quotes.
Code:
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()