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'

  • 3
    `'22.024.833,02'.replace('.','').replace(',','.')` –  Dec 23 '21 at 20:18
  • Does this answer your question? [How can I convert a string with dot and comma into a float in Python](https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python) – Renat Dec 23 '21 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. – CoffeeTableEspresso Dec 23 '21 at 20:20
  • @CoffeeTableEspresso, it's still a number in a localized format, which is addressed in that question – Renat Dec 23 '21 at 20:26
  • @Renat you're correct, I did not see the answer that dealt with this initially when looking at that question – CoffeeTableEspresso Dec 23 '21 at 20:28
  • Answered here https://stackoverflow.com/a/56114791/11687201 just set additional parameters for the file reading with pandas. – ai2ys Dec 23 '21 at 20:31
  • Does this answer your question? [How to convert a string to a number if it has commas in it as thousands separators?](https://stackoverflow.com/questions/1779288/how-to-convert-a-string-to-a-number-if-it-has-commas-in-it-as-thousands-separato) – mkrieger1 Dec 23 '21 at 20:36

3 Answers3

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

CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
  • 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 Dec 23 '21 at 21:00
  • @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. – CoffeeTableEspresso Dec 24 '21 at 06:03
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.

  • 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!!! – leandro stimoli Dec 23 '21 at 21:04
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(',', '.'))
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28