2

Hi I'm trying to solve a problem. I have a json list of products, ex:

[{
    "id": 5677240,
    "name": "Cønjuntø de Pænelæs æntiæderentes ¢øm 05 Peçæs Pæris",
    "quantity": 21,
    "price": "192.84",
    "category": "Panelas"
  },

  {
    "id": 9628920,
    "name": "Lava & Seca 10,2 Kg Sæmsung E¢ø ßußßle ßræn¢æ ¢øm 09 Prøgræmæs de Lævægem",
    "quantity": 57,
    "price": 3719.70,
    "category": "Eletrodomésticos"
  }]

But I basically need the "price" to be float like the second product. I have a large list of these products

(Ignore the weird characters I managed to fix it with help from a teacher.) I converted them to python object using this

import json

with open('br2.json', 'r', encoding='utf8') as json_data:
    data = json.load(json_data)

I've tried something like this but it doesn't work

for product in data:
    product["price"] = product["price"].replace(",", "")

I want to replace the values that are in string with the "" to float

thanks in advance sorry I'm new to python so I don't understand much

0

1 Answer 1

2

You can convert a string to float with float(). So instead of your replace line, try:

product['price'] = float(product['price'])
Sign up to request clarification or add additional context in comments.

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.