[英]Python: decode encoded string to utf-8 inside json file
我有超過 1GB 的 json 文件,里面有編碼的字符串。 例如:
{
"id": "3",
"billing_type": {
"id": "standard",
"name": "\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442"
},
"area": {
"id": "1",
"name": "\u041c\u043e\u0441\u043a\u0432\u0430"
}
}
在我的情況下,我如何在我的 json 文件中像這樣解碼\М\о
字符串?
如果您使用 python3,只需import json
就會有所幫助。
import json
result = json.loads(json_data)
print(result)
或python2,您應該對每個值使用encode
方法(先檢查類型后)
result = json.loads(json_data)
for k, v in result.items():
if isinstance(v, dict):
for dk, dv in v.items():
print dk.encode("utf-8"), dv.encode("utf-8")
else:
print k.encode("utf-8"), v.encode("utf-8")
data = "\u041c\u043e\u0441\u043a\u0432\u0430"
data = data.encode().decode('unicode-escape')
這可能是一個解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.