Python json格式化打印实现过程解析

 更新时间:2020年07月21日 09:17:22   作者:捷后愚生  
这篇文章主要介绍了Python json格式化打印实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

编写python脚本,调试的时候需要打印json格式报文,直接打印看不出层次,可以使用json.dumps格式化打印

import json
import requests

def test_json():
  r=requests.get('https://home.testing-studio.com/categories.json')
  print(r.json())
  print(json.dumps(r.json(), indent=2,ensure_ascii=False)) # r.json()是json对象,indent表示缩进,ensure_ascii设置编码
格式化打印前:

格式化打印前:

格式化打印后:

json.dumps方法源码:

def dumps(obj, *, 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`` to a JSON formatted ``str``.

  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 return value 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.

  """
  # cached encoder
  if (not skipkeys and ensure_ascii and
    check_circular and allow_nan and
    cls is None and indent is None and separators is None and
    default is None and not sort_keys and not kw):
    return _default_encoder.encode(obj)
  if cls is None:
    cls = JSONEncoder
  return cls(
    skipkeys=skipkeys, ensure_ascii=ensure_ascii,
    check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    separators=separators, default=default, sort_keys=sort_keys,
    **kw).encode(obj)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python可视化神器pyecharts绘制折线图详情

    Python可视化神器pyecharts绘制折线图详情

    这篇文章主要介绍了Python可视化神器pyecharts绘制折线图详情,折线图和柱状图一样是我们日常可视化最多的一个图例,当然它的优势和适用场景相信大家肯定不陌生,要想快速的得出趋势,抓住趋势二字,就会很快的想到要用折线图来表示了
    2022-07-07
  • Django项目中表的查询的操作

    Django项目中表的查询的操作

    这篇文章主要介绍了Django项目中表的查询的操作,文中给大家提到了Django项目 ORM常用的十三种查询方法,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • CentOS 7下安装Python3.6 及遇到的问题小结

    CentOS 7下安装Python3.6 及遇到的问题小结

    这篇文章主要介绍了CentOS 7下安装Python3.6 及遇到的问题小结,需要的朋友可以参考下
    2018-11-11
  • 解决pytorch rnn 变长输入序列的问题

    解决pytorch rnn 变长输入序列的问题

    这篇文章主要介绍了解决pytorch rnn 变长输入序列的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • 使用python将图片格式转换为ico格式的示例

    使用python将图片格式转换为ico格式的示例

    今天小编就为大家分享一篇使用python将图片格式转换为ico格式的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Python中计算相似度的方法详解

    Python中计算相似度的方法详解

    计算相似度是许多机器学习和数据分析任务中的重要步骤,尤其是在推荐系统、文本分析和图像处理等领域,下面我们就来看看具体的实现方法吧
    2025-02-02
  • python3爬取数据至mysql的方法

    python3爬取数据至mysql的方法

    这篇文章主要为大家详细介绍了python3爬取数据至mysql的方法 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Python Tkinter基础控件用法

    Python Tkinter基础控件用法

    这篇文章主要介绍了Python Tkinter基础控件用法,包括窗口的显示、显示内置图片、弹出窗口、菜单等等,需要的朋友可以参考下
    2014-09-09
  • Python中有趣在__call__函数

    Python中有趣在__call__函数

    这篇文章主要介绍了Python中有趣在__call__函数,本文直接给出一个使用实例,以此来讲解__call__函数的用法,需要的朋友可以参考下
    2015-06-06
  • python调用百度通用翻译API的方法实现

    python调用百度通用翻译API的方法实现

    本文主要介绍了python调用百度通用翻译API的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-01-01

最新评论