EDIT :
with open("example.csv", "r", encoding="utf-8", errors="ignore") as new_data:
reader = csv.reader(new_data, delimiter=',', quotechar='"')
for row in reader:
if row:
if row[1] is not None:
columns = (row[0], row[1].replace(",","."), row[2])
ean = row[0]
print(row[1])
prices = float(row[1].replace(",","."))
desc = row[2]
#if prices is not None:
result[ean].append((prices, desc))
But I'm still getting strange ouput :
C:\Users\User\AppData\Local\Programs\Python\Python37-32\python.exe
C:/Users/User/AppData/Local/Programs/Python/Python37-32/get_min_price.py
Traceback (most recent call last):
4,43
File "C:/Users/User/AppData/Local/Programs/Python/Python37-
32/get_min_price.py", line 17, in <module>
4,08
13,30
14,90
prices = float(row[1].replace(",","."))
9,31
5,02
4,19
ValueError: could not convert string to float:
4,13
16,57
19,95
8,06
5,99
8,06
For the needs of a min function, I have to convert a list of string to float. However I can't make it works :
result = defaultdict(list)
with open("example.csv", "r", encoding="utf-8", errors="ignore") as new_data:
reader = csv.reader(new_data, delimiter=',', quotechar='"')
for row in reader:
if row:
columns = (row[0], row[1].replace('"','').replace(',','.').strip(), row[2])
ean = row[0]
print (row[1])
prices = float(row[1])
desc = row[2]
if prices is not None:
result[ean].append((prices, desc)) #avoid getting an empty price as minimum value
Output :
4,43
Traceback (most recent call last):
File "C:/Users/User/AppData/Local/Programs/Python/Python37-
32/get_min_price.py", line 16, in <module>
prices = float(row[1])
ValueError: could not convert string to float: '4,43'
Is it because of the comma ? or is there another reason ? (you may also notice that I added a second "replace" which isn't considered by Python ?)
Input example :
3596206198001,"4,43",A
3596206198001,"4,08",B