【问题标题】:Python pp a Json config file [closed]Python pp一个Json配置文件[关闭]
【发布时间】:2021-02-26 18:17:33
【问题描述】:

我尝试漂亮地打印一个 JSON 文件,就像我有一个登录系统并且“数据”应该保存在那里。 但是如果我使用

          with open('authentication.json', 'w+') as outfile:
              json.dump(data, outfile)

输出

{"username": "censored", "password": "censored"}

在 JSON 文件中 但它应该在 JSON 中打印这个

{
  "username": "censored",
  "password": "censored"
}

【问题讨论】:

  • 你试过阅读 json.dump() 的文档吗?

标签: python json pretty-print


【解决方案1】:

这应该可行:

    import json
    data = {"username": "censored", "password": "censored"}
    with open('authentication.json', 'w+') as outfile:
        json.dump(data, outfile, indent=4, sort_keys=True)

【讨论】:

  • 谢谢!就是这样 :D 我从没想过它这么容易!
猜你喜欢
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2010-09-13
相关资源
最近更新 更多