Write a function named "csv_to_kvs" that takes a string as a parameter representing the name of a CSV file with 4 columns in the format "string, float, float, float" and returns a new key-value store mapping strings to floating point numbers. The returned key-value store will have one pair for each row in the file with keys from the first column of the CSV file and values from the third column. (My code below)
import csv
def csv_to_kvs(string):
with open(string) as f:
my_file = csv.reader(f)
my_dict = {}
for row in my_file:
my_dict[row[0]] = row[2]
return my_dict
How can I convert the values in the dictionary into floats?
float(row[2])
?a,1,2.0,3
, and it worked perfectly. If it doesn't work on your file, please provide the contents of that file.