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 相关文章推荐
Python模拟百度登录实例详解
Jan 20 Python
python异常和文件处理机制详解
Jul 19 Python
Python中关键字nonlocal和global的声明与解析
Mar 12 Python
pygame加载中文名mp3文件出现error
Mar 31 Python
Python探索之Metaclass初步了解
Oct 28 Python
python sys,os,time模块的使用(包括时间格式的各种转换)
Apr 27 Python
Python3实现腾讯云OCR识别
Nov 27 Python
python 运用Django 开发后台接口的实例
Dec 11 Python
python2.7实现邮件发送功能
Dec 12 Python
python抓取网页内容并进行语音播报的方法
Dec 24 Python
Python中正反斜杠(‘/’和‘\’)的意义与用法
Aug 12 Python
django admin 添加自定义链接方式
Mar 11 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
星际RPG字典
2020/03/04 星际争霸
PHP cron中的批处理
2008/09/16 PHP
php数组函数序列之array_keys() - 获取数组键名
2011/10/30 PHP
php地址引用(php地址引用的效率问题)
2012/03/23 PHP
web站点获取用户IP的安全方法 HTTP_X_FORWARDED_FOR检验
2013/06/01 PHP
PHP中file_get_contents函数抓取https地址出错的解决方法(两种方法)
2015/09/22 PHP
javascript下判断一个元素是否存在的代码
2010/03/05 Javascript
jquery限定文本框只能输入数字即整数和小数
2013/11/29 Javascript
[原创]JQuery 在表单提交之前修改 提交的值
2016/04/14 Javascript
JS生成不重复的随机数组的简单实例
2016/07/10 Javascript
基于JS实现限时抢购倒计时间表代码
2017/05/09 Javascript
JS实现table表格内针对某列内容进行即时搜索筛选功能
2018/05/11 Javascript
JS实现十分钟倒计时代码实例
2018/10/18 Javascript
超详细动手搭建一个VuePress 站点及开启PWA与自动部署的方法
2019/01/27 Javascript
js数据类型转换与流程控制操作实例分析
2019/12/18 Javascript
windows下create-react-app 升级至3.3.1版本踩坑记
2020/02/17 Javascript
原生js实现碰撞检测
2020/03/12 Javascript
python登录pop3邮件服务器接收邮件的方法
2015/04/30 Python
python嵌套函数使用外部函数变量的方法(Python2和Python3)
2016/01/31 Python
浅谈python对象数据的读写权限
2016/09/12 Python
Python读写Json涉及到中文的处理方法
2016/09/12 Python
浅谈python中的正则表达式(re模块)
2017/10/17 Python
python代码过长的换行方法
2018/07/19 Python
python代码实现图书管理系统
2020/11/30 Python
使用CSS实现弹性视频html5案例实践
2012/12/26 HTML / CSS
松下电器美国官方商店:Panasonic美国
2016/10/14 全球购物
不用游标的SQL语句有哪些
2012/09/07 面试题
行政求职信
2014/07/04 职场文书
2014年9.18纪念日演讲稿
2014/09/14 职场文书
公务员群众路线专题民主生活会发言材料
2014/09/17 职场文书
党的群众路线查摆剖析材料
2014/10/10 职场文书
大学生档案自我鉴定(2篇)
2014/10/14 职场文书
获奖感言一句话
2015/07/31 职场文书
HR在给员工开具离职证明时,需要注意哪些问题?
2019/07/03 职场文书
8g内存用python读取10文件_面试题-python 如何读取一个大于 10G 的txt文件?
2021/05/28 Python
MySQL8.0升级的踩坑历险记
2021/11/01 MySQL