简体   繁体   中英

How to write human-readable data to to a JSON file

["

When I export a file from python to json file it contains charecters like,<\/i>

{"-": "text", "menu": {"-": "node", "id": 2244676, "prev": "[2/40] \u0d2a\u0d4d\u0d30\u0d2f\u0d4b\u0d1c\u0d15 \u0d15\u0d4d\u0d30\u0d3f\u0d2f
["

If you want the output JSON to be human-readable, use UTF-8 encoding and the ensure_ascii=False<\/code> parameter:<\/i>

with open('messages.json', 'w', encoding='utf8') as outfile:
        json.dump(all_messages, outfile, cls=DateTimeEncoder,ensure_ascii=False)

If you want the output JSON to be human-readable, use UTF-8 encoding and the ensure_ascii=False parameter:

with open('messages.json', 'w', encoding='utf8') as outfile:
        json.dump(all_messages, outfile, cls=DateTimeEncoder,ensure_ascii=False)

If you just want to read the data back in again, json.load will convert it back to Unicode:

with open('messages.json', encoding='utf8') as infile:
    data = json.load(infile)

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