-2

so I have a python dictionary in the form of {'key1':[value1, value2]}, I hope to write it to a json file in the format of:

{
   "key1": [value1, value2],
   "key2": [value, value],
   ....
}

I am currently using json.dump(dictionary, file_to_write_to), and the output looks like:

{"key1": [value1, value2], "key2": [value, value]}

2 Answers2

0

Use json pretty print as defined here:

https://docs.python.org/3/library/json.html

haboutnnah
  • 1,118
  • 11
  • 19
0

From simplejson documentation (see pretty print) https://simplejson.readthedocs.io/en/latest/

>>> import simplejson as json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4 * ' '))
{
    "4": 5,
    "6": 7
}
Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
  • This will produce result like this, which is not what I expected. ` "google.com": [ 17.359, 42.282 ], ` – Shulin Chen Oct 24 '16 at 00:14
  • Ahh the format of the comment I made above just messed up. Basically 17.359 and 42.282 got split into two separate lines, which is not what i expected it to be – Shulin Chen Oct 24 '16 at 00:17