我是Python的新手,我正在尝试获取我的名为python_diction的字典(位于一个更大的文件中)的内容,并将这些数据保存到一个新的名为python_diction_saved.json的新文件中。我觉得我已经很接近了,我目前收到的错误是python_diction_saved.json没有定义。
任何帮助都将不胜感激。
import json
f = open("python_diction.txt","w")
python_diction = {}
python_diction["items"] = []
python_diction["items"].append({"hello":1,"hi":2})
python_diction["items"].append({"hello":42,"hi":65})
python_diction["numbers"] = [1,2,3,5,7,8]
f.write(json.dumps(python_diction_saved.json))
f.close()发布于 2017-02-24 17:52:10
为了将python_diction写入名为python_diction_saved.json的文件,您可以使用json.dump (以避免必须自己编写它):
import json
python_diction = {}
python_diction["items"] = []
python_diction["items"].append({"hello":1,"hi":2})
python_diction["items"].append({"hello":42,"hi":65})
python_diction["numbers"] = [1,2,3,5,7,8]
with open("python_diction_saved.json") as output_file:
json.dump(python_diction, output_file)https://stackoverflow.com/questions/42417803
复制相似问题