n00bprogrammer22 n00bprogrammer22 - 21 days ago 6
Python Question

Reading csv file and returning as dictionary

I've written a function that currently reads a file correctly but there are a couple of problems. It needs to be returned as a dictionary where the keys are artist names and the values are lists of tuples (not sure about this but that appears to be what its asking)

The main problem I'm having is that I need to somehow skip the first line of the file and I'm not sure if I'm returning it as a dictionary. Here is an example of one of the files:

"Artist","Title","Year","Total Height","Total Width","Media","Country"
"Pablo Picasso","Guernica","1937","349.0","776.0","oil paint","Spain"
"Vincent van Gogh","Cafe Terrace at Night","1888","81.0","65.5","oil paint","Netherlands"
"Leonardo da Vinci","Mona Lisa","1503","76.8","53.0","oil paint","France"
"Vincent van Gogh","Self-Portrait with Bandaged Ear","1889","51.0","45.0","oil paint","USA"
"Leonardo da Vinci","Portrait of Isabella d'Este","1499","63.0","46.0","chalk","France"
"Leonardo da Vinci","The Last Supper","1495","460.0","880.0","tempera","Italy"


So I need to read an input file and convert it into a dictionary that looks like this:

sample_dict = {
"Pablo Picasso": [("Guernica", 1937, 349.0, 776.0, "oil paint", "Spain")],
"Leonardo da Vinci": [("Mona Lisa", 1503, 76.8, 53.0, "oil paint", "France"),
("Portrait of Isabella d'Este", 1499, 63.0, 46.0, "chalk", "France"),
("The Last Supper", 1495, 460.0, 880.0, "tempera", "Italy")],
"Vincent van Gogh": [("Cafe Terrace at Night", 1888, 81.0, 65.5, "oil paint", "Netherlands"),
("Self-Portrait with Bandaged Ear",1889, 51.0, 45.0, "oil paint", "USA")]
}


The main problem I'm having is skipping the first line that says "Artist","Title", etc. and only returning the lines after the first line. I'm also not sure if my current code is returning this as a dictionary. Here's what I have so far

def convertLines(lines):
head = lines[0]
del lines[0]
infoDict = {}
for line in lines: #Going through everything but the first line
infoDict[line.split(",")[0]] = [tuple(line.split(",")[1:])]
return infoDict

def read_file(filename):
thefile = open(filename, "r")
lines = []
for i in thefile:
lines.append(i)
thefile.close()
mydict = convertLines(read_file(filename))
return lines


Would just a couple small changes to my code return the correct result or would I need to approach this differently? It does appear that my current code reads the full file but how would I skip the first line and possibly return in dict representation if it isnt already? Thanks for any help

Answer

First thing we do is delete the first line of the list.

Then we run a function to do exactly as you say, make a dictionary with list of tuples as values.

You can keep the function you have and run this operation on the lines variable.

Alright run the following code and you should be good

def convertLines(lines):
    head = lines[0]
    del lines[0]
    infoDict = {}
    for line in lines: #Going through everything but the first line
        infoDict[line.split(",")[0]] = [tuple(line.split(",")[1:])]
    return infoDict

def read_file(filename):
    thefile = open(filename, "r")
    lines = []
    for i in thefile:
        lines.append(i)
    thefile.close()
    return lines

mydict = convertLines(read_file(filename))
print(mydict)
#Do what you want with mydict below this line
Comments