Home > Mobile >  Python read last line only but I want all even with the same name
Python read last line only but I want all even with the same name

Time:10-10

I'm trying to write a funtion that takes a txt file filled with movies and ratings into dictionary but the output it came out only read the last line of the same name This is what I had so far

def read_ratings_data(f):
    input = {}
    for line in open(f):
        movie, rating, numId = line.split('|')
        input[movie] = rating
    print(input)

A sample from the txt file:

Toy Story (1995)|4.0|1
Toy Story (1995)|4.0|5
Toy Story (1995)|4.5|7
Toy Story (1995)|2.5|15
Toy Story (1995)|4.5|17
Toy Story (1995)|3.5|18
Jumanji (1995)|4.0|6
Jumanji (1995)|4.0|8
Jumanji (1995)|3.0|18
Jumanji (1995)|3.0|19
Jumanji (1995)|3.0|20
Jumanji (1995)|3.5|21

My output:

{'Toy Story (1995)': '3.5', 'Jumanji (1995)': '3.5'}

Structure of expected output:

{"The Lion King (2019)" : [6.0, 7.5, 5.1], "Titanic (1997)": [7]}

It's suppose to store all the information including movies with same name. It only took the last line of the same name

CodePudding user response:

Based on the expected output you've provided in the comments, you need to initialize the dictionary with a list and then append each rating considering the movie as the key.
You code was not appending the current value but it was replacing it every time the same movie was read.

def read_ratings_data(f):
    movieData = {}
    for line in open(f):
        movie, rating, numId = line.split('|')
        if movieData.get(movie):
            movieData[movie].append(rating)
        else:
            movieData[movie] = [rating]
    print(movieData )

Output

{'Toy Story (1995)': ['4.0', '4.0', '4.5', '2.5', '4.5', '3.5'], 'Jumanji (1995)': ['4.0', '4.0', '3.0', '3.0', '3.0', '3.5']}

CodePudding user response:

Based on your comments I understand that you want the movie name as a key in the dictionary to represent a list of the movie's ratings in the text file:

def read_ratings_data(f):
    movies = dict()
    # use a context manager so that it takes care
    # of exceptions and automatically closes the file
    with open(f) as file:
        for line in file:
            movie, rating, _ = line.strip().split('|')
            # if the dictionary already has the movie
            # it also means it has a corresponding list 
            # so just append to that list the rating
            if movie in movies.keys():
                movies[movie].append(rating)
            else:
                # if there is not yet a key
                # with the movie name
                # create a new key and as
                # the value use a list that contains
                # the rating
                movies[movie] = [rating]
    return movies


print(read_ratings_data('text.txt'))

'text.txt' would be the file where you have the data

CodePudding user response:

You can try this after cleaning your keys, because dicts have only a unique key type:

def read_ratings_data(f):
    input_ = {}
    for line in open(f,'r'):
        movie, rating, numId = line.split('|')
        input_.update({movie:rating})
    print(input_)

Other suggestion given in comments is to iterate by with open(f,'r') as file: and then perform your iteration step line by line. You dont need to use f.close(), because the with statement makes the file f to live only inside himself.

maybe you need this:

import pandas as pd
col_names = ['movie','rating','numId']
s = pd.read_csv(f,names=col_names,sep='|')[['movie','rating']].to_records() 
# you can change .to_record() with .to_dict()
print(s)

output:

[( 0, 'Toy Story (1995)', 4. ) ( 1, 'Toy Story (1995)', 4. )
 ( 2, 'Toy Story (1995)', 4.5) ( 3, 'Toy Story (1995)', 2.5)
 ( 4, 'Toy Story (1995)', 4.5) ( 5, 'Toy Story (1995)', 3.5)
 ( 6, 'Jumanji (1995)', 4. ) ( 7, 'Jumanji (1995)', 4. )
 ( 8, 'Jumanji (1995)', 3. ) ( 9, 'Jumanji (1995)', 3. )
 (10, 'Jumanji (1995)', 3. ) (11, 'Jumanji (1995)', 3.5)]
  • Related