2016-04-04 48 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)。縮進是這裏的關鍵。解決方案簡單