0

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
Greenseed
  • 39
  • 7
  • Please show an example of the input data. You shouldn't need to replace quotes. Anyway, Python isn't ignoring your replace calls, you are; you assign them to `columns` but then subsequently ignore that and just reference the original row attributes. – Daniel Roseman Oct 29 '18 at 15:40
  • 1
    Use dot instead of comma. Change `4,43` to `4.43 ` – Dawid Fieluba Oct 29 '18 at 15:40
  • You need to use the `locale` library for European currency conversion. See https://stackoverflow.com/a/40717213/719547. – Jim Stewart Oct 29 '18 at 15:41
  • Possible duplicate of [How to convert Euro currency string to float number?](https://stackoverflow.com/questions/40717037/how-to-convert-euro-currency-string-to-float-number) – Jim Stewart Oct 29 '18 at 15:41
  • You call 'replace' to get the columns but it doesn't change row[1] inline. You have to call it again before casting to float: `prices = float(row[1].replace(',','.'))` – Tony Pellerin Oct 29 '18 at 15:42
  • @DanielRoseman i just edited to provide an example ! And TonyPellerin: I tried prices = float(row[1].replace(",", ".")) but I'm getting : ValueError: could not convert string to float: – Greenseed Oct 29 '18 at 15:50

3 Answers3

1

Convert the string to the proper floating format, with a .:

prices = float(row[1].replace(",", "."))
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
1

The ValueError should show the wrong input after the error. It should look like this:

ValueError: could not convert string to float: '4,43'

The fact that you are getting nothing after the colon shows that you are actually passing nothing - ie an empty string - into the float function in the first place. This is almost certainly because one row in your CSV - probably the last row - is empty at at that point. If that's the case, you should add a check for the empty string before trying to proceed.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I tried to add a check in the beginning because youre right there is empty values. However I don't think I'm good still. I updated my post – Greenseed Oct 29 '18 at 16:13
0

I didn't intended to answer my own question but it may help some other beginners. If you're stuck on the same point, just track the error like this :

 try:
price = float(price)
 except:
print(float)

Because as said previously you may have some blocking lines or strange inputs you didn't track if you have huge amount of data.

You can correct it or just ignore the mistake with something like this :

 try:
price = float(price)
 except (TypeError, ValueError):
continue

Be careful also that you're using dot instead of coma (e.g 1.6 and not 1,6)

Greenseed
  • 39
  • 7