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
6
  • 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. Commented Oct 29, 2018 at 15:40
  • 1
    Use dot instead of comma. Change 4,43 to 4.43 Commented Oct 29, 2018 at 15:40
  • You need to use the locale library for European currency conversion. See stackoverflow.com/a/40717213/719547. Commented Oct 29, 2018 at 15:41
  • Possible duplicate of How to convert Euro currency string to float number? Commented Oct 29, 2018 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(',','.')) Commented Oct 29, 2018 at 15:42

3 Answers 3

1

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

prices = float(row[1].replace(",", "."))
Sign up to request clarification or add additional context in comments.

4 Comments

I have the same output.. prices = float(row[1].replace(",", ".")) ValueError: could not convert string to float:
It works, but then you are reusing row that still has the wrong character instead of columns.
I changed the CSV input in order to get a dot instead of a coma. However I've still the same message : ValueError: could not convert string to float: 9.31
Are you using row or columns? The latter is your post processed string, row still has the ".
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.

1 Comment

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
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.