Format value in Pyhton

Asked

Viewed 98 times

1

I have a Crawler where I get values in this format: "R$ 450,000.00".

But I need to convert these values into float and save in the database to make queries of this type: Take the values that are between 40,000.00 and 100,000.00.

  • Put what you have. The part that takes the data and saves it in the database.

  • The answer needs to include the section related to the database, or just the conversion of types?

  • First thing you need to know: http://answall.com/q/44715/101

1 answer

1


You can do it like this:

valorRaw = 'R$ 450,000.00'
valor = float(valor.split('$')[1].replace(',', '')) # 450000.0

Assuming you’re sure the values always come in that format

OR:

valorRaw = 'R$ 450,000.00'
valor = float(valor[2:].replace(',', '')) # 450000.0

[2:] means that we want everything from index 2 ("$") and then remove the comma if there is

Browser other questions tagged

You are not signed in. Login or sign up in order to post.