存储输出时 JSON 文件中的垃圾值

garbage value in JSON file while storing the output

当我在网上抓取网站并存储输出时,我发现我的数字数据正在被某种乱码数据所取代。我不知道该怎么办。

json.dumps(lst)
with open('data.json', 'w') as f:
json.dump(lst , f,indent=4)

这就是我存储数据的方式

[
{
    "product_title": "Skybags Brat Black 46 Cms Casual Backpack",
    "MRP": "\u20b92,010.00",
    "Discounted_price": "\u20b9969.00"
},
[

这就是数据的样子

更新:

json.dumps(lst, ensure_ascii=False) 添加到我的代码后遇到此错误

这不是胡言乱语。这是一个 Unicode 字符。您可以在 json.dumps()

中使用 ensure_ascii=False
import json
l = [
{
    "product_title": "Skybags Brat Black 46 Cms Casual Backpack",
    "MRP": "\u20b92,010.00",
    "Discounted_price": "\u20b9969.00"
    }
]

x = json.loads(json.dumps(l, ensure_ascii=False))
print(x)
[
{
'product_title': 'Skybags Brat Black 46 Cms Casual Backpack',
 'MRP': '₹2,010.00', 
'Discounted_price': '₹969.00'
}
]

将数据按原样写入文件。使用这个

with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(lst , f,indent=4,ensure_ascii=False)

那是因为 json.dump 总是读作确保 ascii 您可以将 json.dump(lst , f,indent=4) 更改为 json.dump(lst , f,indent=4, ensure_ascii=False)