theta
theta

Reputation: 25631

Update dictionary while parsing CSV file

I have csv file like this:

item,#RGB
item1,#ffcc00
item1,#ffcc00
item1,#ff00cc
item2,#00ffcc
item2,#ffcc00
item2,#ffcc00
item2,#ffcc00
....

and I want to make dictionary d, with item name as key and RGB value and count as tuples in list as dictionary value, like:

d[item] = [ (#RGB, count) ]

so for "item1" as in example, I would like to get:

d['item1'] = [ ('#ffcc00', 2), ('#ff00cc', 1) ]

I imagine some Pythonic iterator can do this in one line, but I can't understand how at this moment. So far I've made this:

d={}
with open('data.csv', 'rb') as f:
    reader = csv.reader(f)
    try:
        for row in reader:
            try:
                if d[(row[0], row[1])]:
                    i +=1
            except KeyError:
                i = 1
            d[(row[0], row[1])] = i
    except csv.Error, e:
        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))

which gives me:

d[(item, #RGB)] = count

Any better way? Or am I doing this wrongly from start?

Upvotes: 0

Views: 140

Answers (2)

John La Rooy
John La Rooy

Reputation: 304443

import csv
from collections import defaultdict, Counter
from itertools import islice

with open('infile.txt') as f:
    d=defaultdict(Counter)
    for k,v in islice(csv.reader(f),1,None):
        d[k].update((v,))

print d

prints

defaultdict(<class 'collections.Counter'>, {'item2': Counter({'#ffcc00': 3, '#00ffcc': 1}), 'item1': Counter({'#ffcc00': 2, '#ff00cc': 1})})

Upvotes: 0

georg
georg

Reputation: 215039

how about:

a = {}

for row in reader:
    a.setdefault(row[0], {}).setdefault(row[1], 0)
    a[row[0]][row[1]] += 1

This creates a dictionary like

 {'item2': {'#00ffcc': 1, '#ffcc00': 3}, 
  'item1': {'#ffcc00': 2, '#ff00cc': 1}}

I find it more convenient than your structure, but you can convert it to tuples if needed:

 b = dict((k, v.items()) for k, v in a.items())

Upvotes: 2

Related Questions