[Solved] open csv file in python to customize dictionary [duplicate]


First, do you know about the csv module? If not, read the introduction and examples to the point where you know how to get an iterable of rows. Print each one out, and they’ll look something like this:

['AVNIVGYSNAQGVDY', '123431', '27.0', 'Tetramer']

Or, if you use a DictReader:

{'Epitope': 'AVNIVGYSNAQGVDY', 'ID': '123431', 
 'Frequency': '27.0', 'Assay': 'Tetramer'}

But you don’t want a list of lists, or a list of dictionaries; you want a dictionary of lists, where each key is the Epitope from a row, and the corresponding value is the other three values, right? So… just insert each row into a dictionary that way:

d = {}
with open(path, 'rb') as f:
    with csv.DictReader(f) as reader:
        for row in reader:
            d[row['Epitope']] = [row['ID'], row['Frequency'], row['Assay']]

(This would be more compact with a plain reader instead of a DictReader, but I think it’s more readable this way if you come back to the code a few months from now.)

You also want to convert the values and use them as keys for another lookup, but that should be trivial. For example:

            row_id = ID[int(row['ID'])]
            row_frequency = Frequency[float(row['Frequency'])]
            row_assay = Assay[row['Assay']]
            d[row['Epitope']] = [row_id, row_frequency, row_assay]

You can clean this up a bit by writing the row transformation as a function, and then just using a dictionary comprehension:

with open(path, 'rb') as f:
    with csv.DictReader(f) as reader:
        d = {row['Epitope']: process_row(row) for row in reader}

solved open csv file in python to customize dictionary [duplicate]