简体   繁体   中英

How to resolve ValueError in following code?

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

Already tried submitting and altering values but line number 10 always gives value error and so does line number 15. When a list of new nominees is applied, it gives the error and fails my 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()

The error message given is:

Testing a new nominees file. Your submission raised an exception of type ValueError. This occurred on line 10 of program.py.

The above number of condition checks, splitting, concatenation can be omitted with regular expression. You can make use of the below code with a single regular expression and a split

import re
with open("nominees.csv") as cf:
    lines = cf.readlines()

for line in lines[1:]:
    reg_match = re.match(r'"([^""]*)","([^""]*)"$', line)
    if reg_match:
        win_title, director = reg_match.group(1), reg_match.group(2)
    else:
        win_title, director = line.split(",")
    print("Winning title: %s" % win_title)
    print("Congratulations: %s" % director.strip())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM