Pretty print JSON dumps
问题 I use this code to pretty print a dict into JSON: import json d = {'a': 'blah', 'b': 'foo', 'c': [1,2,3]} print json.dumps(d, indent = 2, separators=(',', ': ')) Output: { "a": "blah", "c": [ 1, 2, 3 ], "b": "foo" } This is a little bit too much (newline for each list element!). Which syntax should I use to have this: { "a": "blah", "c": [1, 2, 3], "b": "foo" } instead? 回答1: Write your own JSON serializer: import numpy INDENT = 3 SPACE = " " NEWLINE = "\n" def to_json(o, level=0): ret = "" if