「Python」ディクショナリをJSONにエンコードしてファイルに書き込むサンプル
説明
json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
サンプルコード
# coding: utf-8 #!/usr/bin/python3 import json jsonA = {"username":"user001", "age":"23", "addr":"chang fatun", "female":"woman"} cftFile = open('arkgame.json','w') json.dump(jsonA,cftFile,ensure_ascii=False,indent=4) cftFile.close()
実行結果
arkgame.json
{ "username": "user001", "age": "23", "addr": "chang fatun", "female": "woman" }