【发布时间】:2021-10-07 03:19:14
【问题描述】:
当我在网上抓取网站并存储输出时,我观察到我的数字数据正在被某种乱码数据所取代。我不知道该怎么办。
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) 添加到我的代码后遇到此错误
【问题讨论】:
-
不是专家,但这看起来像字符编码
-
是的,我想有什么解决办法吗?
-
一个快速的解决方法是在写入 json 文件之前去除 unicode 字符,即
json.dumps(lst, ensure_ascii=False).encode('ascii', 'ignore') -
nicodeEncodeError: 'charmap' codec can't encode character '\u20b9' in position 1: character maps to
在将上述代码应用于我的脚本后遇到此错误
标签: python json web-scraping utf-8