1

I have a csv file which looks like this after executing the following code:

with open('XYZ.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
    print(row)

Output:

OrderedDict([('', '0'), ('img_id', '0359a'), ('f1', '2'), ('f2', '1'), ('f3', '1'), ('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')]) OrderedDict([('', '1'), ('img_id', '0577a'), ('f1', '2'), ('f2', '1'), ('f3', '1'), ('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '1'), ('f9', '2')]) OrderedDict([('', '2'), ('img_id', '1120a'), ('f1', '2'), ('f2', '1'), ('f3', '1'), ('f4', '3'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')])

How do I create a dictionary that looks like this:

{
 '0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'), 
 '0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2'), 
 '1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2')
 }

My code is :

d = {}
with open('XYZ.csv') as csvfile:
reader = csv.DictReader(csvfile)
for i in reader:
    for j in i.keys():
        if j in cols:
            d[i['img_id']] = i[j]
print(d)

This is yielding me:

{'0359a': '2', '0577a': '2', '1120a': '2'}

How do I avoid this overwriting?

3 Answers 3

1

This is possible with a simple dictionary comprehension as follows (see explanation in comments):

lines = [
    OrderedDict([('', '0'), ('img_id', '0359a'), ('f1', '2'), ('f2', '1'), ('f3', '1'), 
    ('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')]),
    OrderedDict([('', '1'), ('img_id', '0577a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
    ('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '1'), ('f9', '2')]),
    OrderedDict([('', '2'), ('img_id', '1120a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
    ('f4', '3'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')])]

# for our new dictionary
# key is img_id value in OrderedDict
# and value is a list of all values in OrderedDict if their key isn't '' or 'img_id'
d = {l['img_id']: tuple([v for k, v in l.items() if k not in ('', 'img_id')]) for l in lines}
print(d)

This gives us:

{'0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'), 
 '1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2'), 
 '0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2')}
Sign up to request clarification or add additional context in comments.

Comments

0

you could use a defaultdict with each key being a d[i['img_id']] and the value being a list that you keep appending to

    from collections import defaultdict
    d = defaultdict(list)
    ...
    d[i['img_id']].append(i[j])

Comments

0

You can use the following dict comprehension that unpacks the keys and values from the dict items:

{k: tuple(i for _, i in v) for _, (_, k), *v in (d.items() for d in reader)}

This returns:

{'0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'), '0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2'), '1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2')}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.