how to create a dictionary from csv file?

Hasan Diwan hasandiwan+usenet at gmail.com
Tue Apr 26 20:36:13 EDT 2016


>> I am learning python.
>> 
>> if I have a csv file, like this
>> banana,4.0
>> apple,3.5
>> orange,3.0
>> 
>> Can anyone show me how to read the csv file line by line and then
>> create a dictionary to contain these keys and values?

with open('data.csv') as f: 
    data = dict([[l.strip() for l in line] for line in csv.reader(f)])

data
 {'apple': '3.5', 'banana': '4.0', 'orange': '3.0'}



More information about the Python-list mailing list