Python使用 json.dumps(json_dict, indent=1)中的indent参数指定json字典换行缩进。类似的还可以使用json.dump(json_dict, fp, indent=1)

Prefer = {"jim": {"War"1.9"the big bang"1.0"The lord of wings"4.0"Beautiful America"4.7}, "lily": {"War"2.0"Kongfu"4.1"The lord of wings"3.6}, "tommy": {"War"2.3"Kongfu"5.0"The lord of wings"3.0}, "jack": {"War"2.8"Kongfu"5.5"The lord of wings"3.5}}

print('将字典一行显示不换行')

print(json.dumps(Prefer))

print('将字典按照json样式可视化显示')

print(json.dumps(Prefer,indent=1))

显示的结果是:

将字典一行显示不换行
{"jim": {"War": 1.9, "the big bang": 1.0, "The lord of wings": 4.0, "Beautiful America": 4.7}, "lily": {"War": 2.0, "Kongfu": 4.1, "The lord of wings": 3.6}, "tommy": {"War": 2.3, "Kongfu": 5.0, "The lord of wings": 3.0}, "jack": {"War": 2.8, "Kongfu": 5.5, "The lord of wings": 3.5}}
将字典按照json样式可视化显示
{
  "jim": {
   "War": 1.9,
   "the big bang": 1.0,
   "The lord of wings": 4.0,
   "Beautiful America": 4.7
  },
  "lily": {
   "War": 2.0,
   "Kongfu": 4.1,
   "The lord of wings": 3.6
  },
  "tommy": {
   "War": 2.3,
   "Kongfu": 5.0,
   "The lord of wings": 3.0
  },
  "jack": {
   "War": 2.8,
   "Kongfu": 5.5,
   "The lord of wings": 3.5
  }
}

函数定义:

def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
        allow_nan=True, cls=None, indent=None, separators=None,
        default=None, sort_keys=False, **kw):
    """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
    ``.write()``-supporting file-like object).

    If ``skipkeys`` is true then ``dict`` keys that are not basic types
    (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
    instead of raising a ``TypeError``.

    If ``ensure_ascii`` is false, then the strings written to ``fp`` can
    contain non-ASCII characters if they appear in strings contained in
    ``obj``. Otherwise, all such characters are escaped in JSON strings.

    If ``check_circular`` is false, then the circular reference check
    for container types will be skipped and a circular reference will
    result in an ``OverflowError`` (or worse).

    If ``allow_nan`` is false, then it will be a ``ValueError`` to
    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
    in strict compliance of the JSON specification, instead of using the
    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).

    If ``indent`` is a non-negative integer, then JSON array elements and
    object members will be pretty-printed with that indent level. An indent
    level of 0 will only insert newlines. ``None`` is the most compact
    representation.

    If specified, ``separators`` should be an ``(item_separator, key_separator)``
    tuple.  The default is ``(', ', ': ')`` if *indent* is ``None`` and
    ``(',', ': ')`` otherwise.  To get the most compact JSON representation,
    you should specify ``(',', ':')`` to eliminate whitespace.

    ``default(obj)`` is a function that should return a serializable version
    of obj or raise TypeError. The default simply raises TypeError.

    If *sort_keys* is true (default: ``False``), then the output of
    dictionaries will be sorted by key.

    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
    ``.default()`` method to serialize additional types), specify it with
    the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.

    """

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐