Compact but pretty JSON output in python?

◇◆丶佛笑我妖孽 提交于 2019-12-04 03:42:16

问题


JSON is written either with indent=None (default) as a single line (unreadable to a human eye) or with ident=N with a newline after each comma.

What I would like to see is a more compact but still pretty output, similar to what Common Lisp pretty-printing does. E.g., instead of

 {
  "cleanup":{
   "cpu":6936.780000000001,
   "wall":7822.319401979446
  },
  "finished":"2017-08-14 18:36:23",
  "init":{
   "cpu":1365.73,
   "wall":1380.7802910804749
  },
  "job":"timings",
  "run":{
   "cpu":953.6700000000001,
   "wall":8350.496850013733
  },
  "started":"2017-08-14 13:28:06"
 }

I would like to see

 {
  "cleanup":{"cpu":6936.780000000001,"wall":7822.319401979446},
  "finished":"2017-08-14 18:36:23",
  "init":{"cpu":1365.73,"wall":1380.7802910804749},
  "job":"timings",
  "run":{"cpu":953.6700000000001,"wall":8350.496850013733},
  "started":"2017-08-14 13:28:06"
 }

similar to what pprint produces.


回答1:


This is not possible at this time, but when RFE: more compact pretty printing is implemented, this question will be answered by using python-rapidjson.



来源:https://stackoverflow.com/questions/45681702/compact-but-pretty-json-output-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!