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


Posted in Python onJuly 21, 2020

编写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设置编码
格式化打印前:

格式化打印前:

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

格式化打印后:

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

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 相关文章推荐
Windows系统下使用flup搭建Nginx和Python环境的方法
Dec 25 Python
Python使用Paramiko模块编写脚本进行远程服务器操作
May 05 Python
Python Socket使用实例
Dec 18 Python
python中将\\uxxxx转换为Unicode字符串的方法
Sep 06 Python
使用Selenium破解新浪微博的四宫格验证码
Oct 19 Python
django mysql数据库及图片上传接口详解
Jul 18 Python
tensorflow2.0与tensorflow1.0的性能区别介绍
Feb 07 Python
Tensorflow 卷积的梯度反向传播过程
Feb 10 Python
浅谈在JupyterNotebook下导入自己的模块的问题
Apr 16 Python
Keras设置以及获取权重的实现
Jun 19 Python
Python的控制结构之For、While、If循环问题
Jun 30 Python
通过实例解析Python RPC实现原理及方法
Jul 07 Python
基于python实现删除指定文件类型
Jul 21 #Python
python打开音乐文件的实例方法
Jul 21 #Python
Python读取yaml文件的详细教程
Jul 21 #Python
Python中bisect的用法及示例详解
Jul 20 #Python
python为什么要安装到c盘
Jul 20 #Python
python如何代码集体右移
Jul 20 #Python
python接入支付宝的实例操作
Jul 20 #Python
You might like
WordPress中邮件的一些修改和自定义技巧
2015/12/15 PHP
php 浮点数比较方法详解
2017/05/05 PHP
php 的多进程操作实践案例分析
2020/02/28 PHP
对xmlHttp对象的理解
2011/01/17 Javascript
AJAX 网页保留浏览器前进后退等功能
2011/02/12 Javascript
jQuery图片轮播的具体实现
2013/09/11 Javascript
js禁止回车提交表单的示例代码
2013/12/23 Javascript
jQuery EasyUI API 中文帮助文档和扩展实例
2016/08/01 Javascript
使用JavaScript触发过渡效果的方法
2017/01/19 Javascript
防止重复发送 Ajax 请求
2017/02/15 Javascript
深入浅出webpack之externals的使用
2017/12/04 Javascript
JavaScript中字符串的常用操作方法及特殊字符
2018/03/18 Javascript
Vue三层嵌套路由的示例代码
2018/05/05 Javascript
通过一次报错详细谈谈Point事件
2018/05/17 Javascript
vue安装遇到的5个报错及解决方法
2019/06/12 Javascript
ant-design-vue 实现表格内部字段验证功能
2019/12/16 Javascript
CKEditor扩展插件:自动排版功能autoformat插件实现方法详解
2020/02/06 Javascript
JS函数本身的作用域实例分析
2020/03/16 Javascript
WebPack工具运行原理及入门教程
2020/12/02 Javascript
python实现数通设备tftp备份配置文件示例
2014/04/02 Python
Python实现的百度站长自动URL提交小工具
2014/06/27 Python
如何安装并在pycharm使用selenium的方法
2020/04/30 Python
HTML5中drawImage用法分析
2014/12/01 HTML / CSS
Hunkemöller瑞士网上商店:欧洲最大的内衣品牌之一
2018/12/03 全球购物
大学生自荐书范文
2013/12/10 职场文书
《石榴》教学反思
2014/03/02 职场文书
联欢晚会主持词
2014/03/25 职场文书
美食节策划方案
2014/05/26 职场文书
个人四风问题原因分析及整改措施
2014/09/28 职场文书
初三学生语文考试作弊检讨书
2014/12/14 职场文书
优秀教师先进事迹材料
2014/12/15 职场文书
2015年财务工作总结范文
2015/03/31 职场文书
大学团日活动总结书
2015/05/11 职场文书
企业版Windows 11有哪些新功能? Win11适用于企业的功能介绍
2021/11/21 数码科技
Redis特殊数据类型Geospatial地理空间
2022/06/01 Redis
Java中的Kafka为什么性能这么快及4大核心详析
2022/09/23 Java/Android