python - Reading csv file and returning as dictionary -


i've written function reads file correctly there couple of problems. needs returned dictionary keys artist names , values lists of tuples (not sure appears asking)

the main problem i'm having need somehow skip first line of file , i'm not sure if i'm returning dictionary. here example of 1 of 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 @ 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 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 need read input file , convert dictionary looks 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 @ night", 1888, 81.0, 65.5, "oil paint", "netherlands"),                              ("self-portrait bandaged ear",1889, 51.0, 45.0, "oil paint", "usa")]       } 

the main problem i'm having skipping first line says "artist","title", etc. , returning lines after first line. i'm not sure if current code returning dictionary. here's have far

def convertlines(lines):     head = lines[0]     del lines[0]     infodict = {}     line in lines: #going through first line         infodict[line.split(",")[0]] = [tuple(line.split(",")[1:])]     return infodict  def read_file(filename):     thefile = open(filename, "r")     lines = []     in thefile:         lines.append(i)     thefile.close()     mydict = convertlines(read_file(filename))     return lines 

would couple small changes code return correct result or need approach differently? appear current code reads full file how skip first line , possibly return in dict representation if isnt already? help

first thing delete first line of list.

then run function say, make dictionary list of tuples values.

you can keep function have , run operation on lines variable.

alright run following code , should good

def convertlines(lines):     head = lines[0]     del lines[0]     infodict = {}     line in lines: #going through first line         infodict[line.split(",")[0]] = [tuple(line.split(",")[1:])]     return infodict  def read_file(filename):     thefile = open(filename, "r")     lines = []     in thefile:         lines.append(i)     thefile.close()     return lines  mydict = convertlines(read_file(filename)) print(mydict) #do want mydict below line 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -