2

from excel I have a number in the following type: "22.024.833,02 ", so I've been trying .strip(), .replace() among others but I couldn't get a float from my code.

for columna, fila in mav.iterrows():
    comitente = fila['Comit.']
    moneda = fila['Especie']
    m = fila['Operado'].strip()
    m2 = m.replace('.', '')
    monto = float(m2)
    

Result: monto = float(m2) ValueError: could not convert string to float: '22024833,02'

7
  • 3
    '22.024.833,02'.replace('.','').replace(',','.') Commented Dec 23, 2021 at 20:18
  • Does this answer your question? How can I convert a string with dot and comma into a float in Python Commented Dec 23, 2021 at 20:19
  • @Renat, it looks like OP is using the dot for separating digits and the comma for the decimal point, so that answer doesn't quite get there. Commented Dec 23, 2021 at 20:20
  • @CoffeeTableEspresso, it's still a number in a localized format, which is addressed in that question Commented Dec 23, 2021 at 20:26
  • @Renat you're correct, I did not see the answer that dealt with this initially when looking at that question Commented Dec 23, 2021 at 20:28

3 Answers 3

2

My answer is assuming you're using . for digit separator and , for the decimal point.

m = fila['Operado'].strip()    # m = "22.024.833,02"
m = m.replace('.', '')         # m = "22024833,02"
m = m.replace(',', '.')        # m = "22024833.02"
monto = float(m)               # monto = 22024833.02

Python's float expects no digit separator (the . in your example), and the decimal point to be a . (not a , as in your input).

Sign up to request clarification or add additional context in comments.

2 Comments

The answer is about string->float conversion, not about getting a single string from a csv file's row. Btw I found really usefull to read the comments next to your code to better understand what the OP was really asking. +1
@FLAK-ZOSO You're right, reading from the file is not important at all, I just formatted it this way to align with what was in OPs post.
1

I think this is what you are looking for

m = float(fila['Operado'].strip().replace(".","").replace(",","."))

Its better to use . for decimal places and , for the whole number part.

1 Comment

I had also tried replacing "," for "." but it didnt work guys, i tried it again and it worked! I appreciatate your comments , thank you guys!!!
0

In Python you should use . instead of , as decimal separator.

good_float = 1.44
not_a_float = 1,22 # This will be stored as a tuple

If your input string is written with , as decimal separator, you can use String.replace() method.

>>> '3,14'.replace(',', '.')
'3.14'

If you are also using . as digit separator, you can replace it with the same method.

>>> '1.203,14'.replace('.', ' ').replace(',', '.')
'1203,14'

Then you can finely convert the string to a float using the float built-in function.

>>> float('3,14'.replace('.', ' ').replace(',', '.'))
3.14

Remember to always write .replace('.', ' ') before .replace(',', '.') to avoid a mess with periods.


Your code should work now:

>>> float(fila['Operado'].strip().replace('.', ' ').replace(',', '.'))

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.