python, writing Json to file [duplicate] python, writing Json to file [duplicate] json json

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 use with open..., then the handler is always closed properly.
  • result = load(file) should be result = file.read()