Greenseed
Greenseed

Reputation: 39

String to float python -incorrect format?

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

Upvotes: 0

Views: 1038

Answers (3)

Greenseed
Greenseed

Reputation: 39

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)

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599630

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.

Upvotes: 1

Matthieu Brucher
Matthieu Brucher

Reputation: 22023

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

prices = float(row[1].replace(",", "."))

Upvotes: 1

Related Questions