I keep getting an error saying my index value is out of range. Here is the text file I am using:
The Lion King (2019): 6.0
The Lion King (2019): 7.5
The Lion King (2019): 5.1
Titanic (1997): 7
code:
#function that will return the dictionary
def read_ratings_data(f):
#to store the lines read from the file
lines = []
#defining the dictionary
movie_ratings_dict = {}
#open read file
#read lines and store
#closes file
with open (f, "r") as file:
lines = file.readlines()
file.close()
#should remove \n symbol
#converts text into list of movie rating
#ISSUE: keep getting an error with the third line not sure how to fix
for i in range(len(lines)):
temp = lines[i][: -1].split(".")
lines[i] = [temp[0], float(temp[1])]
#stores data into dict
for i in lines:
#if statement when there is a new movie in the text
#will create a new list for that movie (key)
if i[0] not in movie_ratings_dict:
movie_ratings_dict[i[0]] = []
#appends the rating as in, as we encounter new ratings, will add to the end
movie_ratings_dict[i[0]].append(i[1])
#return dict ending
return movie_ratings_dict
read_ratings_data("movie_ratings.txt")
CodePudding user response:
You've made this far more complicated than it needs to be.
The input file seems to have the film title terminated by ':'. Therefore we need the tokens either side of that - i.e., the title and the rating.
The objective is to create a dictionary keyed on film names with values which are a list of all known ratings.
So, open the file and read a line at a time splitting each line into its two component parts.
Use setdefault on the list. If the first parameter given to setdefault (the key) doesn't exist then the default value (in this case an empty list) will be returned and associated with that key in the dictionary.
Even though the second token (the rating) will have a newline as its last character, we can take advantage of the fact that float() is impervious to whitespace. So, for example, float('1.5\n') will return 1.5
def read_ratings_data(filename):
result = {}
with open(filename) as file:
for line in file:
title, rating = line.split(':')
result.setdefault(title, []).append(float(rating))
return result
print(read_ratings_data('ratings.txt'))
Output:
{'The Lion King (2019)': [6.0, 7.5, 5.1], 'Titanic (1997)': [7.0]}
CodePudding user response:
You are facing error as when you split "Titanic (1997): 7" with delimiter ".", list of element is not created as there is no "." character. Hence the error to get temp[1] Check the len() of temp variable, and then assign it based on requirement.
In Python, "for i in lines" is not same as in C/C . here variable "i" will store the full string based on each iteration.
if i[0] not in movie_ratings_dict: can be replaced with if i not in movie_ratings_dict: similarly at other places.
CodePudding user response:
You should make a split based on the :
delimiter.
Just change this part of the code and it will work fine.
for i in range(len(lines)):
temp = lines[i].split(":")
lines[i] = [temp[0], float(temp[1])]