python, writing Json to file [duplicate]
You can use json.dump()
method:
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:
- don't need to do
file.close()
. If you usewith open...
, then the handler is always closed properly. result = load(file)
should beresult = file.read()