2
0

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 1 year has passed since last update.

Pythonで jsonファイル読み書き(UTF-8)

Last updated at Posted at 2022-10-23

Unicodeエスケープなし そして
jsonファイルが読みやすいように改行やスペースをいれてファイルに書き込む

import json

# 読み込み
def read(path):
    with open(path, 'r', encoding="utf-8") as file:
        return json.load(file)

# 書き込み
def save(path, data):
    with open(path, 'w', encoding='utf-8') as file:
        json.dump(data, file, indent=4, ensure_ascii=False)

jsonファイルを読み込む時

json_data = read("ファイルの場所")

で json_dataという変数に代入されます

jsonファイルを書き込む時

save("ファイルの場所","保存したい内容")

で 指定したファイルに保存できます

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?