'How to create a function to save information in dictionary from a csv file in python

I am writing and testing a function for opening a .csv file and save information in a dictionary, instead of using import csv.

The csv file is like:

16SAP,12/02/24,sapin-9,MATEYIJDNS
FAS1,01/02/21,fasiculata,MTYEUSOLD
EDS5,10/20/20,epsilon,MHGSJDKDLSKDKDJS
etc....

and .csv file has a (,) separated format and 4 fields: identifier, date, name and sequence, respectively.

My code is:

def dicfromcsv(csv_file):
    with open('csv_file', 'r') as f:
        d = {}
        l = f.read().split(',')
        for i in l:
            values = i.split(':')
            d[values[0]] = values[1], values[2], values[3], values[4]

dicfromcsv('PDB.csv')

But it doesn't function.

Thank in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source