【问题标题】:Python json config filePython json 配置文件
【发布时间】:2016-04-04 19:33:55
【问题描述】:

我写了json配置文件:

{
"file": {"file_name": "file.xls"}, 
"names1": 
    {"Joe Allen": "JOE", 
     "Leo Messi": "MESSI"}
}

它是格式化和可读的。我使用函数来更改文件名:

def plik(self, evt):
    wildcard = "MS excell (*.xls)|*.xls"
    dialog = wx.FileDialog(self, "Choose a file", os.getcwd(), "",     wildcard, wx.OPEN)
    if dialog.ShowModal() == wx.ID_OK:
        config['file'] = {'file_name': dialog.GetFilename()}
        with open('config.json', 'w') as f:
            json.dump(config, f)
        self.Destroy()
        frame = Program()
        frame.Show()

修改后的 json 文件文本如下:

{"file": {"file_name": "file.xls"},"names1":{"Joe Allen": "JOE","Leo Messi": "MESSI"}}

我应该怎么做才能继续格式化。

【问题讨论】:

    标签: python json


    【解决方案1】:

    我是通过json.dump(config, f, sort_keys=True, indent=2) 完成的。缩进是这里的关键。简单的解决方案。

    【讨论】:

      猜你喜欢
      • 2016-10-19
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 2019-01-14
      • 2014-03-27
      • 1970-01-01
      • 2018-10-23
      相关资源
      最近更新 更多