I am trying to make a dictionary from a csv file in python. Let's say the CSV contains:
Student food amount
John apple 15
John banana 20
John orange 1
John grape 3
Ben apple 2
Ben orange 4
Ben strawberry 8
Andrew apple 10
Andrew watermelon 3
what i'm envisioning is a dictionary whose key will be the student name and a list as the value where each entry corresponds to a different food. I would have to count the number of unique food items in the second column and that would be the length of the vector. For example:
The value of [15,20,1,3,0,0] would correspond to [apple, banana, orange, grape, strawberry, watermelon] for 'John'.
The value of [2,0,4,0,8,0] would correspond to [apple, banana, orange, grape, strawberry, watermelon] for 'Ben'.
The value of [10,0,0,0,0,3] would correspond to [apple, banana, orange, grape, strawberry, watermelon] for 'Andrew'
The expected output of the dict would look like this:
dict={'John':{[15,20,1,3,0,0]}, 'Ben': {[2,0,4,0,8,0]}, 'Andrew': {[10,0,0,0,0,3]}}
I'm having trouble creating the dictionary to begin with or if a dictionary is even the right approach. What I have to begin with:
import csv
data_file=open('data.csv','rU')
reader=csv.DictReader(data_file)
data={}
for row in reader:
data[row['Student']]=row
data_file.close()
thanks for taking the time to read. any help would be greatly appreciated.