【发布时间】:2021-07-09 17:16:05
【问题描述】:
我正在尝试将用户输入的数据存储在 dict 中的 .json 文件中。我正在使用
d = data
with open('file.json', 'a') as save_file:
convert_file.write(json.dumps(d))
但是当我尝试用
打印数据时with open('file.json') as f:
filedata = [json.loads(line) for line in f]
print(filedata)
我收到一个错误,因为 json 文件搞砸了。
{"dict1": {"a": ["1", "2"], "Name": "test"}}{"dict2": {"a": ["3", "4"], "Name": "test2"}}
如何将我的字典写入文件而不弄乱格式?
【问题讨论】:
标签: python json dictionary