Read a csv file and add your data in a dictionary

1

I have a code where I will have a function that will pass the data from a csv file to a dictionary. The process will take the movies with their respective notes and add to a dictionary relative to each user, and add those dictionaries to a main dictionary.

import csv

path = 'C:\Users\Rafael\Desktop\DataMining\RECOMMENDATION\Movie_Ratings.csv'
data = {}

def loadCSV():
    file = open(path, newline='')
    reader = csv.reader(file)
    header = next(reader)
    header.remove('')
    n = 0  
    for u in header:
        n += 1
        data[u] = {}
        for line in reader:
            data[u][line[0]] = line[n]
loadCSV()
print(data)

However, when I run my code ...

{'Patrick C': {'Alien': '', 'Avatar': '4', 'Blade Runner': '5', 'Braveheart': '4', 'Dodgeball': '5', 'Forest Gump': '4', 'Gladiator': '', 'Jaws': '', 'Kazaam': '2', 'Lord of the Rings': '4', 'Napolean Dynamite': '3', 'Old School': '3', 'Pootie Tang': '', 'Pulp Fiction': '', 'Scarface': '', 'Shawshank Redemption': '5', 'Snakes on a Plane': '4', 'Spiderman': '4', 'Star Wars': '5', 'The Dark Knight': '4', 'The Happening': '1', 'The Matrix': '3', 'Toy Story': '4', 'Village': '', 'You Got Mail': '1'}, 'Heather': {}, 'Bryan': {}, 'Patrick T': {}, 'Thomas': {}, 'aaron': {}, 'vanessa': {}, 'greg': {}, 'brian': {}, 'ben': {}, 'Katherine': {}, 'Jonathan': {}, 'Zwe': {}, 'Erin': {}, 'Chris': {}, 'Zak': {}, 'Matt': {}, 'Josh': {}, 'Amy': {}, 'Valerie': {}, 'Gary': {}, 'Stephen': {}, 'Jessica': {}, 'Jeff': {}}

... Only the first user receives the movie notes. Please HELP !!

    
asked by anonymous 08.09.2018 / 01:50

2 answers

1

As I have not been informed of the structure of the CSV, I will assume that it is structured like this:

Readingafileisalwaysincremental,whencsv.reader()iscalled,thereturnisaniteratorandnotalist.Inthiscase,theiteratorworksusinganinternalcountertoaccesstherowsofthefile,ifthefilehas100rows,whentherowsfinishexecutesthecounterwillbeat100,howeverifyoutrytoexecuteanotherfor,thecounterwillstartin100andnot0,soonlythefirstuserhasevaluations.

Sotoreadthedatafromthefile,thecodeneedstobechangedlikethis:

importcsvpath='C:\Users\Rafael\Desktop\DataMining\RECOMMENDATION\Movie_Ratings.csv'data=[]defloadCSV():file=open(path,newline='')reader=csv.reader(file)header=next(reader)header.remove('')forlineinreader:item={"filme": "",
            "avaliacoes": []
        }

        for index, valor in enumerate(line):
            if index == 0:
                # Primeiro item da lista = nome do filme
                item["filme"] = valor
            else:
                # Demais itens = avaliacao de usuario
                """
                Como o nome do usuario ta no header, na mesma sequencia
                em que os itens são acessados, basta passar o index - 1,
                para saber de quem foi a nota
                """
                item["avaliacoes"].append({
                    "usuario": header[index - 1],
                    "nota": valor
                })

        data.append(item)

loadCSV()
print(data)

The result of the above code looks like this:

[
  {
    "filme": "Alien",
    "avaliacoes": [
      {
        "usuario": "Patrick C",
        "nota": 1
      },
      {
        "usuario": "Heather",
        "nota": 2
      },
      {
        "usuario": "Bryan",
        "nota": 3
      },
    ]
  },
  {
    "filme": "Avatar",
    "avaliacoes": [
      {
        "usuario": "Patrick C",
        "nota": 3
      },
      {
        "usuario": "Heather",
        "nota": 2
      },
      {
        "usuario": "Bryan",
        "nota": 1
      },
    ]
  },
  ...
]
    
08.09.2018 / 02:31
1

Your code did not work because you are trying to replay the file:

for u in header:
    # ...
    for line in reader:

The first time will work, but the second, reader has already been consumed. If your code works as it is, you would have to "rewind" the file, or store the content in a list.

    
08.09.2018 / 19:42