繁体   English   中英

如何将人类可读的数据写入 JSON 文件

[英]How to write human-readable data to to a JSON file

["

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

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

如果您希望输出 JSON 是人类可读的,请使用 UTF-8 编码和ensure_ascii=False参数:

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

如果您只想再次读回数据, json.load会将其转换回 Unicode:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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