1

I wrote json configuration file:

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

It is formatted and readable. I use function to change 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()

After change text in json file looks like:

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

What should I do to keep formating.

jundymek
  • 1,013
  • 4
  • 18
  • 40

1 Answers1

0

I did it by json.dump(config, f, sort_keys=True, indent=2). Indent was a key here. Simple solution.

jundymek
  • 1,013
  • 4
  • 18
  • 40