Help us understand the problem. What is going on with this article?

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

More than 5 years have 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"])
tadokoro
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away