I'm tring to create a dictionary importing data from an excel file converted in csv and I want to convert the string value of the dictionary into float, but I get in return this error ValueError: invalid literal for float(): 437,33
import csv
from collections import defaultdict
my_dict = {}
my_dict = defaultdict(lambda : 0, my_dict)
with open('excel_csv_file.csv', 'rb') as file_object:
reader = csv.reader(file_object, delimiter=';')
for x in reader:
my_dict[(x[0], x[1])] = x[2]
my_dict = dict((k, float(v)) for k,v in my_dict.iteritems())
print my_dict
This is what my_dict looks like
{('11605', 'TV'): '437,33',
('10850', 'SMARTPHONE'): '163,47',
('11380', 'TV'): '1911,72',
('11177', 'SMARTPHONE'): '255,80',
('11237', 'TABLET'): '382,28',
('11238', 'TABLET'): '458,01',
('11325', 'TABLET'): '309,55',
...}
Why am I getting this error?
Also, is there a way to convert the string value inside the tuple key into an int? (for instance ('11605', 'TV') to (11605, 'TV'))?
,to separate the integer and fraction parts of a number. Python expects.for that.