如何在使用JSON模块进行漂亮打印时实现自定义缩进?

69

所以我正在使用Python 2.7版本,使用json模块对以下数据结构进行编码:

'layer1': {
    'layer2': {
        'layer3_1': [ long_list_of_stuff ],
        'layer3_2': 'string'
    }
}

我的问题是,我正在使用漂亮的打印方式将所有内容都打印出来,如下所示:

json.dumps(data_structure, indent=2)

这很好,但我想要将它全部缩进,除了"layer3_1"中的内容——它是一个包含坐标的大字典列表,因此每个值设置为单个值会使漂亮的打印创建一个具有成千上万行的文件,例如:

{
  "layer1": {
    "layer2": {
      "layer3_1": [
        {
          "x": 1,
          "y": 7
        },
        {
          "x": 0,
          "y": 4
        },
        {
          "x": 5,
          "y": 3
        },
        {
          "x": 6,
          "y": 9
        }
      ],
      "layer3_2": "string"
    }
  }
}

我真正想要的是类似于以下内容:

{
  "layer1": {
    "layer2": {
      "layer3_1": [{"x":1,"y":7},{"x":0,"y":4},{"x":5,"y":3},{"x":6,"y":9}],
      "layer3_2": "string"
    }
  }
}

我听说可以扩展 json 模块: 是否可以将其设置为仅在 "layer3_1" 对象内部时关闭缩进? 如果是这样,有人能告诉我如何吗?


7
你的第一个代码片段既不是 JSON 也不是 Python。 - user647772
缩进是打印的问题,而不是表示的问题。 - Yuval Adam
你说的“pretty printing”是指使用pprint模块吗? - Bakuriu
修改了第一个片段,使其可识别。 而且我正在使用 json.dumps(data_structure, indent=2) - 将其作为示例添加进去。 - Rohaq
我已经发布了一个解决方案,适用于2.7版本,并且与sort_keys等选项很好地配合使用,而且没有特殊情况的排序实现,而是依赖于(组合)collections.OrderedDict - Erik Kaplun
12个回答

29

(注意: 本答案提供的代码仅适用于返回JSON格式字符串的json.dumps()函数,而不适用于将数据直接写入文件的json.dump()函数。有一个修改版的代码同时适用于两种情况,请参考我在这个问题下的回答。)

更新

以下是经过多次修订的原始回答版本。与我最初发布的版本不同的是,我最初发布的版本只是为了展示如何实现J.F.Sebastian在此回答中提出的第一个想法,并且与他的回答一样,返回了非缩进的字符串表示对象。最新的更新版本会单独返回Python对象的JSON格式。

每个坐标点的dict键将按排序后的顺序显示,这是根据OP的评论指定的,但仅当在驱动该过程的初始json.dumps()调用中指定了sort_keys=True关键字参数时才会进行排序,并且它不再在路上更改对象的类型为字符串。换句话说,现在“包装”的对象的实际类型得到了维护。

我认为,由于不理解我发布帖子的原始意图,导致许多人对它进行了负面投票——因此,主要是出于这个原因,我已经多次“修复”和改进了我的回答。当前版本是我最初答案的混合体,结合了@Erik Allik在他的回答中提出的一些思路,以及下面评论中其他用户给出的有用反馈。

以下代码似乎在Python 2.7.16和3.7.4中均可使用。

from _ctypes import PyObj_FromPtr
import json
import re

class NoIndent(object):
    """ Value wrapper. """
    def __init__(self, value):
        self.value = value


class MyEncoder(json.JSONEncoder):
    FORMAT_SPEC = '@@{}@@'
    regex = re.compile(FORMAT_SPEC.format(r'(\d+)'))

    def __init__(self, **kwargs):
        # Save copy of any keyword argument values needed for use here.
        self.__sort_keys = kwargs.get('sort_keys', None)
        super(MyEncoder, self).__init__(**kwargs)

    def default(self, obj):
        return (self.FORMAT_SPEC.format(id(obj)) if isinstance(obj, NoIndent)
                else super(MyEncoder, self).default(obj))

    def encode(self, obj):
        format_spec = self.FORMAT_SPEC  # Local var to expedite access.
        json_repr = super(MyEncoder, self).encode(obj)  # Default JSON.

        # Replace any marked-up object ids in the JSON repr with the
        # value returned from the json.dumps() of the corresponding
        # wrapped Python object.
        for match in self.regex.finditer(json_repr):
            # see https://dev59.com/jmUp5IYBdhLWcg3w1qIi#15012814
            id = int(match.group(1))
            no_indent = PyObj_FromPtr(id)
            json_obj_repr = json.dumps(no_indent.value, sort_keys=self.__sort_keys)

            # Replace the matched id string with json formatted representation
            # of the corresponding Python object.
            json_repr = json_repr.replace(
                            '"{}"'.format(format_spec.format(id)), json_obj_repr)

        return json_repr


if __name__ == '__main__':
    from string import ascii_lowercase as letters

    data_structure = {
        'layer1': {
            'layer2': {
                'layer3_1': NoIndent([{"x":1,"y":7}, {"x":0,"y":4}, {"x":5,"y":3},
                                      {"x":6,"y":9},
                                      {k: v for v, k in enumerate(letters)}]),
                'layer3_2': 'string',
                'layer3_3': NoIndent([{"x":2,"y":8,"z":3}, {"x":1,"y":5,"z":4},
                                      {"x":6,"y":9,"z":8}]),
                'layer3_4': NoIndent(list(range(20))),
            }
        }
    }

    print(json.dumps(data_structure, cls=MyEncoder, sort_keys=True, indent=2))

输出:

{
  "layer1": {
    "layer2": {
      "layer3_1": [{"x": 1, "y": 7}, {"x": 0, "y": 4}, {"x": 5, "y": 3}, {"x": 6, "y": 9}, {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11, "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17, "s": 18, "t": 19, "u": 20, "v": 21, "w": 22, "x": 23, "y": 24, "z": 25}],
      "layer3_2": "string",
      "layer3_3": [{"x": 2, "y": 8, "z": 3}, {"x": 1, "y": 5, "z": 4}, {"x": 6, "y": 9, "z": 8}],
      "layer3_4": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    }
  }
}

不错,我已经把它搞定了,但是出于虚荣心的原因(JSON 的某些部分需要稍后手动编辑,别问为什么:()),我想对 x 和 y 进行排序,所以我尝试使用 OrderedDict。现在我的问题是,输出中出现了以下内容:"layer3_1": "[OrderedDict([('x', 804), ('y', 622)]), OrderedDict([('x', 817), ('y', 635)]), OrderedDict([('x', 817), ('y', 664)]), OrderedDict([('x', 777), (' y', 664)]), OrderedDict([('x', 777), ('y', 622)]), OrderedDict([('x', 804), ('y' , 622)])]",我觉得我漏掉了什么... - Rohaq
7
仍然会将列表打印为字符串。 - Erik Kaplun
1
@ErikAllik 是完全正确的。列表变成了一个字符串:"[{'x':1, 'y':7}, {'x':0, 'y':4}, {'x':5, 'y':3}, {'x':6, 'y':9}]"。这是错误的答案! - AnnieFromTaiwan
1
由于使用单引号,无法使用反序列化(json.loads())功能。我必须使用@ErikAllik的答案。-- https://github.com/patarapolw/pyexcel-formatter/blob/master/pyexcel_formatter/serialize.py#L31 - Polv
1
@Polv:感谢您的反馈。我已经更新了我的答案来解决这个问题。 - martineau
显示剩余10条评论

17

虽然不是很优美的方法,但是一旦你从dumps()中获取到字符串后,如果你确定它的内容格式,就可以对其执行正则表达式替换。大致如下:

s = json.dumps(data_structure, indent=2)
s = re.sub('\s*{\s*"(.)": (\d+),\s*"(.)": (\d+)\s*}(,?)\s*', r'{"\1":\2,"\3":\4}\5', s)

谢谢,这个也行,而且确实更小,但我决定采用@martineau提供的解决方案。 - Rohaq
你的解决方案非常有趣!:) 我喜欢它,而且不需要任何“NoIdent”标记,开箱即用。明天我可能会测试它的大型输入文件,我正在寻找一个简单的解决方案来摆脱csv世界,因为它实际上不允许元数据,但保持可读性。 - Barney Szabolcs
嘿,太棒了!我在你的想法基础上提供了一个更通用的解决方案,使用以下正则表达式:re.sub((?:\n\s{8,}(.*))|(?:\n\s{6,}(]|})), r'\1\2', s) 或在 https://regex101.com/r/xWT7I1/2 阅读它。 - Marc Moreaux

13
下面的解决方案似乎在Python 2.7.x上可以正常工作。它使用了一个来自Custom JSON encoder in Python 2.7 to insert plain JavaScript code的变通方法,通过使用基于UUID的替换方案来避免自定义编码对象最终以JSON字符串的形式输出。
class NoIndent(object):
    def __init__(self, value):
        self.value = value


class NoIndentEncoder(json.JSONEncoder):
    def __init__(self, *args, **kwargs):
        super(NoIndentEncoder, self).__init__(*args, **kwargs)
        self.kwargs = dict(kwargs)
        del self.kwargs['indent']
        self._replacement_map = {}

    def default(self, o):
        if isinstance(o, NoIndent):
            key = uuid.uuid4().hex
            self._replacement_map[key] = json.dumps(o.value, **self.kwargs)
            return "@@%s@@" % (key,)
        else:
            return super(NoIndentEncoder, self).default(o)

    def encode(self, o):
        result = super(NoIndentEncoder, self).encode(o)
        for k, v in self._replacement_map.iteritems():
            result = result.replace('"@@%s@@"' % (k,), v)
        return result

那么这个

obj = {
  "layer1": {
    "layer2": {
      "layer3_2": "string", 
      "layer3_1": NoIndent([{"y": 7, "x": 1}, {"y": 4, "x": 0}, {"y": 3, "x": 5}, {"y": 9, "x": 6}])
    }
  }
}
print json.dumps(obj, indent=2, cls=NoIndentEncoder)

生成以下输出:

{
  "layer1": {
    "layer2": {
      "layer3_2": "string", 
      "layer3_1": [{"y": 7, "x": 1}, {"y": 4, "x": 0}, {"y": 3, "x": 5}, {"y": 9, "x": 6}]
    }
  }
}

它还能正确地将所有选项(除了indent)传递给嵌套的json.dumps调用,例如sort_keys=True

obj = {
    "layer1": {
        "layer2": {
            "layer3_1": NoIndent([{"y": 7, "x": 1, }, {"y": 4, "x": 0}, {"y": 3, "x": 5, }, {"y": 9, "x": 6}]),
            "layer3_2": "string",
        }
    }
}    
print json.dumps(obj, indent=2, sort_keys=True, cls=NoIndentEncoder)

正确地输出:

{
  "layer1": {
    "layer2": {
      "layer3_1": [{"x": 1, "y": 7}, {"x": 0, "y": 4}, {"x": 5, "y": 3}, {"x": 6, "y": 9}], 
      "layer3_2": "string"
    }
  }
}

它也可以与例如 collections.OrderedDict 结合使用:

obj = {
    "layer1": {
        "layer2": {
            "layer3_2": "string",
            "layer3_3": NoIndent(OrderedDict([("b", 1), ("a", 2)]))
        }
    }
}
print json.dumps(obj, indent=2, cls=NoIndentEncoder)

输出:

{
  "layer1": {
    "layer2": {
      "layer3_3": {"b": 1, "a": 2}, 
      "layer3_2": "string"
    }
  }
}

更新:在Python 3中,没有 iteritems。您可以用以下代码替换 encode

items()

def encode(self, o):
    result = super(NoIndentEncoder, self).encode(o)
    for k, v in iter(self._replacement_map.items()):
        result = result.replace('"@@%s@@"' % (k,), v)
    return result

4
对于那些不理解这个解决方案如何工作的人:encode()函数内的两行代码 for k, v in self._replacement_map.iteritems(): result = result.replace('"@@%s@@"' % (k,), v) 是用来将 "layer3_1": "@@d4e06719f9cb420a82ace98becab5ff8@@" 替换为 "layer3_1": [{"y": 7, "x": 1}, {"y": 4, "x": 0}, {"y": 3, "x": 5}, {"y": 9, "x": 6}]。我认为这个解决方案在某种意义上等同于 @M Somerville 的正则表达式替换解决方案。 - AnnieFromTaiwan
5
这在Python 3中同样适用。唯一的注意事项是你必须使用json.dumps,而不是json.dump!在后一种情况下,你还需要覆盖iterencode(),但我没能做到这点。 - letmaik

9
这将产生OP所期望的结果:
import json

class MyJSONEncoder(json.JSONEncoder):

  def iterencode(self, o, _one_shot=False):
    list_lvl = 0
    for s in super(MyJSONEncoder, self).iterencode(o, _one_shot=_one_shot):
      if s.startswith('['):
        list_lvl += 1
        s = s.replace('\n', '').rstrip()
      elif 0 < list_lvl:
        s = s.replace('\n', '').rstrip()
        if s and s[-1] == ',':
          s = s[:-1] + self.item_separator
        elif s and s[-1] == ':':
          s = s[:-1] + self.key_separator
      if s.endswith(']'):
        list_lvl -= 1
      yield s

o = {
  "layer1":{
    "layer2":{
      "layer3_1":[{"y":7,"x":1},{"y":4,"x":0},{"y":3,"x":5},{"y":9,"x":6}],
      "layer3_2":"string",
      "layer3_3":["aaa\nbbb","ccc\nddd",{"aaa\nbbb":"ccc\nddd"}],
      "layer3_4":"aaa\nbbb",
    }
  }
}

jsonstr = json.dumps(o, indent=2, separators=(',', ':'), sort_keys=True,
    cls=MyJSONEncoder)
print(jsonstr)
o2 = json.loads(jsonstr)
print('identical objects: {}'.format((o == o2)))

3
你可以尝试以下方法:
  • mark lists that shouldn't be indented by replacing them with NoIndentList:

    class NoIndentList(list):
        pass
    
  • override json.Encoder.default method to produce a non-indented string representation for NoIndentList.

    You could just cast it back to list and call json.dumps() without indent to get a single line

看起来上述方法对于json模块不起作用:

import json
import sys

class NoIndent(object):
    def __init__(self, value):
        self.value = value

def default(o, encoder=json.JSONEncoder()):
    if isinstance(o, NoIndent):
        return json.dumps(o.value)
    return encoder.default(o)

L = [dict(x=x, y=y) for x in range(1) for y in range(2)]
obj = [NoIndent(L), L]
json.dump(obj, sys.stdout, default=default, indent=4)

它会产生无效的输出(列表被序列化为字符串):
[
    "[{\"y\": 0, \"x\": 0}, {\"y\": 1, \"x\": 0}]", 
    [
        {
            "y": 0, 
            "x": 0
        }, 
        {
            "y": 1, 
            "x": 0
        }
    ]
]

如果你能使用yaml,那么这种方法就可行:
import sys
import yaml

class NoIndentList(list):
    pass

def noindent_list_presenter(dumper, data):
    return dumper.represent_sequence(u'tag:yaml.org,2002:seq', data,
                                     flow_style=True)
yaml.add_representer(NoIndentList, noindent_list_presenter)


obj = [
    [dict(x=x, y=y) for x in range(2) for y in range(1)],
    [dict(x=x, y=y) for x in range(1) for y in range(2)],
    ]
obj[0] = NoIndentList(obj[0])
yaml.dump(obj, stream=sys.stdout, indent=4)

它会产生以下结果:
- [{x: 0, y: 0}, {x: 1, y: 0}]
-   - {x: 0, y: 0}
    - {x: 0, y: 1}

即第一个列表使用[]进行序列化,所有项都在同一行上;第二个列表每个项都独占一行。

1
我觉得我理解了你说的一半,但是我还是有点困惑。可能是因为我之前没有在Python中重写方法的经验。我会多读一些资料,但如果你能提供一个更完整的例子,那就太好了! - Rohaq

3

如果您有太多不同类型的对象导致无法尝试使用JSONEncoder方法或使用正则表达式处理,那么这里提供了一种后处理解决方案。此函数在指定的级别后折叠空格,而无需知道数据本身的具体细节。

def collapse_json(text, indent=12):
    """Compacts a string of json data by collapsing whitespace after the
    specified indent level

    NOTE: will not produce correct results when indent level is not a multiple
    of the json indent level
    """
    initial = " " * indent
    out = []  # final json output
    sublevel = []  # accumulation list for sublevel entries
    pending = None  # holder for consecutive entries at exact indent level
    for line in text.splitlines():
        if line.startswith(initial):
            if line[indent] == " ":
                # found a line indented further than the indent level, so add
                # it to the sublevel list
                if pending:
                    # the first item in the sublevel will be the pending item
                    # that was the previous line in the json
                    sublevel.append(pending)
                    pending = None
                item = line.strip()
                sublevel.append(item)
                if item.endswith(","):
                    sublevel.append(" ")
            elif sublevel:
                # found a line at the exact indent level *and* we have sublevel
                # items. This means the sublevel items have come to an end
                sublevel.append(line.strip())
                out.append("".join(sublevel))
                sublevel = []
            else:
                # found a line at the exact indent level but no items indented
                # further, so possibly start a new sub-level
                if pending:
                    # if there is already a pending item, it means that
                    # consecutive entries in the json had the exact same
                    # indentation and that last pending item was not the start
                    # of a new sublevel.
                    out.append(pending)
                pending = line.rstrip()
        else:
            if pending:
                # it's possible that an item will be pending but not added to
                # the output yet, so make sure it's not forgotten.
                out.append(pending)
                pending = None
            if sublevel:
                out.append("".join(sublevel))
            out.append(line)
    return "\n".join(out)

例如,使用此结构作为输入传递给json.dumps,缩进级别为4:
text = json.dumps({"zero": ["first", {"second": 2, "third": 3, "fourth": 4, "items": [[1,2,3,4], [5,6,7,8], 9, 10, [11, [12, [13, [14, 15]]]]]}]}, indent=4)

以下是不同缩进级别下该函数的输出:

>>> print collapse_json(text, indent=0)
{"zero": ["first", {"items": [[1, 2, 3, 4], [5, 6, 7, 8], 9, 10, [11, [12, [13, [14, 15]]]]], "second": 2, "fourth": 4, "third": 3}]}
>>> print collapse_json(text, indent=4)
{
    "zero": ["first", {"items": [[1, 2, 3, 4], [5, 6, 7, 8], 9, 10, [11, [12, [13, [14, 15]]]]], "second": 2, "fourth": 4, "third": 3}]
}
>>> print collapse_json(text, indent=8)
{
    "zero": [
        "first",
        {"items": [[1, 2, 3, 4], [5, 6, 7, 8], 9, 10, [11, [12, [13, [14, 15]]]]], "second": 2, "fourth": 4, "third": 3}
    ]
}
>>> print collapse_json(text, indent=12)
{
    "zero": [
        "first", 
        {
            "items": [[1, 2, 3, 4], [5, 6, 7, 8], 9, 10, [11, [12, [13, [14, 15]]]]],
            "second": 2,
            "fourth": 4,
            "third": 3
        }
    ]
}
>>> print collapse_json(text, indent=16)
{
    "zero": [
        "first", 
        {
            "items": [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                9,
                10,
                [11, [12, [13, [14, 15]]]]
            ], 
            "second": 2, 
            "fourth": 4, 
            "third": 3
        }
    ]
}

这个对我来说非常有效。 - Luis

3

为我和Python 3用户解答

import re

def jsonIndentLimit(jsonString, indent, limit):
    regexPattern = re.compile(f'\n({indent}){{{limit}}}(({indent})+|(?=(}}|])))')
    return regexPattern.sub('', jsonString)

if __name__ == '__main__':
    jsonString = '''{
  "layer1": {
    "layer2": {
      "layer3_1": [
        {
          "x": 1,
          "y": 7
        },
        {
          "x": 0,
          "y": 4
        },
        {
          "x": 5,
          "y": 3
        },
        {
          "x": 6,
          "y": 9
        }
      ],
      "layer3_2": "string"
    }
  }
}'''
    print(jsonIndentLimit(jsonString, '  ', 3))

'''print
{
  "layer1": {
    "layer2": {
      "layer3_1": [{"x": 1,"y": 7},{"x": 0,"y": 4},{"x": 5,"y": 3},{"x": 6,"y": 9}],
      "layer3_2": "string"
    }
  }
}'''

这可能是被接受的答案。要对字典进行漂亮的打印,将其与json.dumps结合使用,看起来像这样:jsonString = json.dumps(thedict, indent=4); print(jsonIndentLimit(jsonString, ' ', 3)) - Jordan He

1

最佳性能代码(10MB文本耗时1秒):

import json
def dumps_json(data, indent=2, depth=2):
    assert depth > 0
    space = ' '*indent
    s = json.dumps(data, indent=indent)
    lines = s.splitlines()
    N = len(lines)
    # determine which lines to be shortened
    is_over_depth_line = lambda i: i in range(N) and lines[i].startswith(space*(depth+1))
    is_open_bracket_line = lambda i: not is_over_depth_line(i) and is_over_depth_line(i+1)
    is_close_bracket_line = lambda i: not is_over_depth_line(i) and is_over_depth_line(i-1)
    # 
    def shorten_line(line_index):
        if not is_open_bracket_line(line_index):
            return lines[line_index]
        # shorten over-depth lines
        start = line_index
        end = start
        while not is_close_bracket_line(end):
            end += 1
        has_trailing_comma = lines[end][-1] == ','
        _lines = [lines[start][-1], *lines[start+1:end], lines[end].replace(',','')]
        d = json.dumps(json.loads(' '.join(_lines)))
        return lines[line_index][:-1] + d + (',' if has_trailing_comma else '')
    # 
    s = '\n'.join([
        shorten_line(i)
        for i in range(N) if not is_over_depth_line(i) and not is_close_bracket_line(i)
    ])
    #
    return s

更新: 这是我的解释:

首先,我们使用json.dumps来获取已缩进的JSON字符串。 例如:

>>>  print(json.dumps({'0':{'1a':{'2a':None,'2b':None},'1b':{'2':None}}}, indent=2))
[0]  {
[1]    "0": {
[2]      "1a": {
[3]        "2a": null,
[4]        "2b": null
[5]      },
[6]      "1b": {
[7]        "2": null
[8]      }
[9]    }
[10] }

如果我们设置 indent=2depth=2,那么深度为 2 的行将以 6 个空格开头。
我们有 4 种类型的行:
  1. 普通行
  2. 开括号行(2,6)
  3. 超过深度的行(3,4,7)
  4. 闭括号行(5,8)
我们将尝试将一系列行(类型 2 + 3 + 4)合并为一行。例如:
[2]      "1a": {
[3]        "2a": null,
[4]        "2b": null
[5]      },

将被合并到:
[2]      "1a": {"2a": null, "2b": null},

注意:关闭括号行可能有尾部逗号。

但是,他们没有问到速度和性能!请进一步解释。 - isaeid
我必须统计一组庞大的数据指标。因此我只关注性能和准确性。 - TRUC Vu

0

这个解决方案并不像其他方案那样优雅和通用,你也不会从中学到太多东西,但它快速简单。

def custom_print(data_structure, indent):
    for key, value in data_structure.items():
        print "\n%s%s:" % (' '*indent,str(key)),
        if isinstance(value, dict):
            custom_print(value, indent+1)
        else:
            print "%s" % (str(value)),

使用方法和输出:

>>> custom_print(data_structure,1)

 layer1:
  layer2:
   layer3_2: string
   layer3_1: [{'y': 7, 'x': 1}, {'y': 4, 'x': 0}, {'y': 3, 'x': 5}, {'y': 9, 'x': 6}]

0

顺便提一下,该网站内置了JavaScript,当行长度小于70个字符时,将避免JSON字符串中出现换行符:

http://www.csvjson.com/json_beautifier

(使用修改版的JSON-js实现)

选择“内联短数组”

非常适合快速查看您在剪贴板中拥有的数据。


2
问题是,如何在Python中实现“内联短数组”? - Polv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接