0

I know there is many questions as mine and I tryied almost all of them but no one of them worked

Here is what I have '380,00\xa0' as a string want to convert it to float

But here is what I get ValueError: could not convert string to float: '380,00\xa0'

I tried str.replace("\xa0", "")

The length of the string is dynamique so I can't do float(str[:5])

Ali
  • 687
  • 8
  • 27
  • Does "380.00" work? Is your culture set for comma as the decimal separator? Try `import locale; print(locale.localeconv());` – Rup Nov 11 '19 at 17:54
  • @Rup yes dont have problem with that! but Ineed whats after the coma – Ali Nov 11 '19 at 17:55

1 Answers1

2

The decimal separator for float is represented by the "." character, not by the "," character. Hence we have to do:

>>> s = "380,00\xa0"
>>> f = float(s.replace("\xa0", "").replace(",", "."))
>>> f

380.0
Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44