pythonでjsonを扱う時、日本語をエスケープさせない方法

More than 1 year has passed since last update.

エンコード

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import json

dict = {"hello": "日本語"}
text = json.dumps(dict, sort_keys=True, ensure_ascii=False, indent=2)
with open("utf8.json", "w") as fh:
    fh.write(text.encode("utf-8"))

デコード

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import json

with open("utf8.json") as fh:
    js = json.loads(fh.read(), "utf-8")
    print(js["hello"])
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.