사전형식을 json 파일로 저장하기





방법1


import json
from pathlib import Path

data = {'aaa': 1629,  'bbb': 1675, 'ccc': 2042}
example_path = Path('aaa.json')
json_str = json.dumps(data, indent=4) + '\n'
example_path.write_text(json_str, encoding='utf-8')
 

{
"aaa": 1629,
"bbb": 1675,
"ccc": 2042
}



방법2


import json
data = {'aaa': 1629,  'bbb': 1675, 'ccc': 2042}
with open('bbb.json', 'w') as fp:
    json.dump(data, fp)
 

{"aaa": 1629, "bbb": 1675, "ccc": 2042}



방법3


import json
data = {'aaa': 1629,  'bbb': 1675, 'ccc': 2042}
with open("ccc.json", 'w') as fout:
    json_dumps_str = json.dumps(data, indent=4)
    print(json_dumps_str, file=fout)
 

{
"aaa": 1629,
"bbb": 1675,
"ccc": 2042
}