將資料儲存在檔案中

以下程式碼段將儲存在 d 中的資料編碼為 JSON 並將其儲存在檔案中(將 filename 替換為檔案的實際名稱)。

import json

d = {
    'foo': 'bar',
    'alice': 1,
    'wonderland': [1, 2, 3]
}

with open(filename, 'w') as f:
    json.dump(d, f)