Reputation: 19
I have a csv file like this
Name Value
a 10
a 20
a 30
b 10
and I want a dictionary like this
words = { a: [10,20,30], b:[10]}
Sorry for the easy question
Upvotes: 1
Views: 52
Reputation: 9345
you can use DictReader
import csv
words = dict()
with open('names.csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter= ' ', skipinitialspace=True)
for row in reader:
words.setdefault(row['Name'], []).append(row['Value'])
Upvotes: 5
Reputation: 1486
You can use defaultdict for this as well
from collections import defaultdict
some= defaultdict(list)
with open('filepath', 'r') as file:
for line in file.readlines():
x,y = line.split(' ')
some[x].append(y.replace('\n',''))
Upvotes: 0