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?