87
82

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

エンコード

# -*- 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"])
87
82
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
87
82

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?