import json
def save_object(obj, file_path):
"""
任意のオブジェクトをJSON形式でファイルに出力する関数。
"""
try:
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(obj, file, ensure_ascii=False, indent=4)
print(f"オブジェクトを {file_path} に保存しました。")
except (TypeError, IOError) as e:
print(f"エラーが発生しました: {e}")
def load_object(file_path):
"""
JSON形式のファイルを読み込み、オブジェクトとして返す関数。
"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
obj = json.load(file)
print(f"{file_path} からオブジェクトを読み込みました。")
return obj
except (json.JSONDecodeError, IOError) as e:
print(f"エラーが発生しました: {e}")
return None
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme