1. 程式人生 > >【程式碼】第5章 資料儲存

【程式碼】第5章 資料儲存

5.1.2 json

import json
info = {
    'name': '王偉',
    'gender': '難',
    'birthday': '1992-10-08'
}
with open('512.json','w', encoding='gb2312') as file:            # indent加入縮排
    file.write(json.dumps(info, indent=2, ensure_ascii=False))   # 避免ASCII/Unicode碼字元
with open('512.json', 'r', encoding='gb2312') as file2: 		 # 可見這裡檔案讀寫預設用gb2312編碼
    print(file2.read())                                 		 # with as保證關閉