简体   繁体   中英

Python: decode encoded string to utf-8 inside json file

I've more then 1GB json file with encoded strings inside. For example:

{
    "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"
    }
}

How I can decode like this \М\о string inside my json file in my case?

if you use python3, just import json will help.

import json


result = json.loads(json_data)
print(result)

or python2, you should use encode method for each values (after check type first)

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')

This might be a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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