编写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)

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

pythonjson格式化输出_pythonjson格式化输出_Python json格式化打印实现过程解析相关推荐

  1. python将print 输出为表格形式_python print 格式化输出

    使用print可以输出的类型有:字符串.整数.浮点数及精度控制. 1.格式化输出整数 python print也支持参数格式化,与C言的printf似: strHello = "the le ...

  2. JSON格式化有问题,Jackson替换JSON格式化处理

    前言描述: 在格式化对象的过程中,经常遇到我们使用阿里巴巴的JSON格式化工具包,可是后来这个包被证明有问题,有风险注入. 目前的替代方案是: Jackson 工具类实现代码JSON格式化代码 imp ...

  3. python 文字转语音 带情感_python文字转语音实现过程解析

    这篇文章主要介绍了python文字转语音实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用百度接口 接口地址https://ai.ba ...

  4. python怎么换行输出的数字对齐_python中格式化输出和字母大小写转换,对齐填充方式...

    #格式化输出 print("ang is a good time") str7="ong is a boy" num=10 f=5.22313 # %d(整数站 ...

  5. python输出一首诗_Python:如何打印我的简单诗

    像往常一样(对我来说),可能还有一种更像Python的方法,但为了让你的工作发挥作用,我做了三件事:将对nomens()函数的调用分配给"choosed\u list"变量.这样就 ...

  6. python格式化代码是什么意思_Python代码格式化

    4 个答案: 答案 0 :(得分:13) 多行字符串更具可读性: def __str__(self): return '''\ Car Type mpg: %.1f hp: %.2f pc: %i u ...

  7. python htmltestrunner报告_Python HTMLTestRunner可视化报告实现过程解析

    操作步骤 1.下载HTMLTestRunner.py 2.把文件复制到python安装/lib位置下 3. 3.导入:import HTMLTestRunner import unittest 4.m ...

  8. python画代码流程图_Python使用graphviz画流程图过程解析

    问题描述 项目中需要用到流程图,如果用js的echarts处理,不同层级建动态计算位置比较复杂,考虑用python来实现 测试demo 实现效果如下 完整代码 import yaml import o ...

  9. python语音合成实现原理_python腾讯语音合成实现过程解析

    一.腾讯语音合成介绍 腾讯云语音合成技术(TTS)可以将任意文本转化为语音,实现让机器和应用张口说话. 腾讯TTS技术可以应用到很多场景,比如,移动APP语音播报新闻:智能设备语音提醒:依靠网上现有节 ...

  10. python execjs详解_Python基于execjs运行js过程解析

    execjs 使用 有了selenium+Chrome Headless 加载页面为什么还要用execjs来运行js? selenium+Chrome Headless 必然是爬虫的一大利器,可是缺点 ...

最新文章

  1. 显示ip的img的url
  2. DK云网关与普通DTU之间的区别
  3. 科研实习 | 中国科学院深圳先进技术研究院徐坤老师课题组招聘客座实习生
  4. python36_第36 p,就这几个方法,Python中文件的操作
  5. get请求可以传body吗_都9102年了,GET和POST的区别掌握了没有?
  6. 湖北2021高考成绩几点可以查询,2021年湖北高考成绩查询时间安排 什么时间出分...
  7. 软件测试-缺陷报告(自己看)
  8. SpringBoot系列: 所有配置属性和官方文档
  9. Flink 1.10 Container 环境实战
  10. 边沿检测电路设计verilog
  11. Qt实现职工信息管理系统
  12. 免费OCR文字识别软件,识别图片文字
  13. 深度学习---卷积神经网络+tensorflow实现
  14. 战胜自己,征服北京四海记
  15. winform实现任务栏隐藏、通知栏图标及右键、类似QQ隐藏窗体等功能
  16. python - 1024 Spider
  17. 至少12亿元收支差,分析运营商7大数据产品应用
  18. 地图处理方法-判断一个点是否在某个区域内
  19. 最小割问题-Karger‘s algorithm
  20. jd脚本 v4-bot 镜像部署和配置bot机器人

热门文章

  1. MacBook安装Mysql
  2. 编程小工具总结(一) 取色器
  3. pycharm2020版本以上中文版教程
  4. linux系统 nvme驱动模块,Linux 5.5最终通过HWMON公开NVMe驱动器温度
  5. iOS App常用的宏
  6. 产品研发测试在敏捷各环节的职责
  7. 二调建设用地地类代码_最新二调土地地类代码表
  8. axios封装接口步骤详解
  9. (软考信息安全工程师--中级)二、网络攻击原理与常用方法
  10. Eclipse安装Lombok