python, writing Json to file
This question already has an answer here:
你可以使用json.dump()方法:
with open("text", "w") as outfile:
json.dump({'numbers':n, 'strings':s, 'x':x, 'y':y}, outfile, indent=4)
Change:
dumps({'numbers':n, 'strings':s, 'x':x, 'y':y}, file, indent=4)
To:
file.write(dumps({'numbers':n, 'strings':s, 'x':x, 'y':y}, file, indent=4))
Also:
file.close() . If you use with open... , then the handler is always closed properly. result = load(file) should be result = file.read() 上一篇: 从python脚本编写Json文件
下一篇: python,写Json文件
