0

How to print the long dictionary in to separate lines

test = {'db1': [{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases ', 'request': 'POST'}], 'db2': [{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases ', 'request': 'POST'}]}

Expected output

test = 
{'db1':[{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases', 'request': 'POST'}],
 'db2': [{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases', 'request': 'POST'}]}

By importing json module is printing is giving the same output as normal print (test)

import json
print (json.dumps(test))

1 Answers1

1

If your just want to "pretty-print" your dictionary, set the indent parameter of the json.dumps function:

>>> import json
>>> test = {'db1': [{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases ', 'request': 'POST'}], 'db2': [{'url': 'http://localhost:8080/api', 'cmd': 'test\\nshow databases ', 'request': 'POST'}]}
>>> print(json.dumps(test, indent=2))
{
  "db1": [
    {
      "url": "http://localhost:8080/api",
      "cmd": "test\\nshow databases ",
      "request": "POST"
    }
  ],
  "db2": [
    {
      "url": "http://localhost:8080/api",
      "cmd": "test\\nshow databases ",
      "request": "POST"
    }
  ]
}
kalehmann
  • 4,821
  • 6
  • 26
  • 36