python, writing Json to file

This question already has an answer here:

  • How do I write JSON data to a file? 10 answers

  • 你可以使用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:

  • 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()
  • 链接地址: http://www.djcxy.com/p/48684.html

    上一篇: 从python脚本编写Json文件

    下一篇: python,写Json文件