繁体   English   中英

Python:将编码的字符串解码为 json 文件中的 utf-8

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM