I have a csv file where I want to query results based on the first row and first column. Given a csv that looks like:
subj_id, Sample1, Sample2, Sample3
XXX, 1, 2, 3
YYY, 4, 5, 6
ZZZ, 7, 8, 9
I would like to use the subj_id
and SampleN
to query the value. For example:
dct['XXX']['Sample1'] = 1, dct['YYY']['Sample2'] = 5
I have tried using csv.DictReader
, but without success. How can I create a dictionary like the above? (Note: I am using python 3.6, as I see that DictReader is slightly different depending on the python version.)
My attempt was based on this SO question, where the start was:
import csv
from collections import defaultdict
dct = defaultdict(dict)
with open('toy_out_test_results.csv', 'r') as csv_file:
for row in csv.DictReader(csv_file, fieldnames=['subj_id','Sample1','Sample2','Sample3']):
dct[row['subj_id']][row['???']] =
but I ran into a roadblock here.