2016-04-04 49 views
0

我写JSON配置文件:Python的JSON配置文件

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

据格式和可读性。我使用功能改变FILE_NAME:

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"}} 

我应该怎么做,以保持格式化。

回答

0

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