将数据存储在文件中

以下代码段将存储在 d 中的数据编码为 JSON 并将其存储在文件中(将 filename 替换为文件的实际名称)。

import json

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

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