python格式化JSON结果打印
重点其实就是dumps函数的参数需要进行调节,一行代码
# json格式化输出
json.dumps(response.json(), sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False)
关于json的dump函数
直接去查看原函数,可以看到:
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 skippedinstead of raising a ``TypeError``.If ``ensure_ascii`` is false, then the return value can contain non-ASCIIcharacters if they appear in strings contained in ``obj``. Otherwise, allsuch characters are escaped in JSON strings.If ``check_circular`` is false, then the circular reference checkfor container types will be skipped and a circular reference willresult in an ``OverflowError`` (or worse).If ``allow_nan`` is false, then it will be a ``ValueError`` toserialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) instrict compliance of the JSON specification, instead of using theJavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).If ``indent`` is a non-negative integer, then JSON array elements andobject members will be pretty-printed with that indent level. An indentlevel of 0 will only insert newlines. ``None`` is the most compactrepresentation.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 versionof obj or raise TypeError. The default simply raises TypeError.If *sort_keys* is true (default: ``False``), then the output ofdictionaries 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 withthe ``cls`` kwarg; otherwise ``JSONEncoder`` is used."""
python格式化JSON结果打印相关推荐
- Python格式化JSON文件
之前工作中有一个需求是要格式化json文件,遇到一个问题,是要把一个json文件里面的内容格式化一下,在此做个记录 import json class JsonFormat(): " ...
- python 格式化Json数据
有2种方式处理Json数据,1是通过自带的json模块,通过json.loads和json.dumps方法进行处理.2是自己写模块处理json模块.本文讲的是第2种. 为什么要自己写模块调用呢,因为遇 ...
- python快速格式化json
一.管道方式 echo '{"a": 1, "b": 2}' | python -m json.tool 二.文件方式 python -m json.tool ...
- python的format用法打印表格_Python格式化输出——format用法示例
format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {} ...
- python 格式化打印print常见用法(更新中)
python 格式化打印print常见用法 1.一般打印 1.1打印到控制台 1.2 打印到文件 2.格式化打印 2.1 % 方法 2.1.1 格式化常用符号 2.1.2 格式化操作符辅助命令 2.2 ...
- 使用Python命令行优美打印json文件
JSON 是一种非常常见的数据格式,但人工读起来比较困难.有的json文件就是非常长的一行文字,没有任何换行符和空格,对人来说,难以理解器内容. Python 2.6 及以上版本,提供了json.to ...
- linux 利用python模块实现格式化json
非json格式示例 {"name": "chen2ha", "where": {"country": "Chi ...
- python -m json.tool 中文乱码 Format JSON with python
python -m json.tool 中文乱码 Format JSON with python 现在以 json 为数据传输格式的 RESTful 接口非常流行.为调试这样的接口,一个常用的办法是使 ...
- python读取中文文件乱码-详解Python的json文件读取及中文乱码显示问题解决方法...
Python的json文件读取及解决中文乱码显示问题 本文实例讲述了Python实现的json文件读取及中文乱码显示问题解决方法.分享给大家供大家参考,具体如下: city.json文件的内容如下: ...
- python simdjson_python+json
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 一.json函数: 使用json函数需要导入json库:import json json.dumps() ...
最新文章
- PCL调错(2):VTK报错
- 12c双机rac oracle_深度干货 | 如何借助云原生搞定Oracle备份快速恢复?
- 最小正连续子序列和 问题
- HTTP/3 未来可期?
- 计算机检索的pdf格式,win10系统实现全文搜索pdf文件的解决办法
- elementUI 分页组件Bug ,为什么会 infinity ?
- vb中SendMessage函数
- 修改密码-测试用例设计
- DispatcherServlet和Spring应用上下文初始化详解
- NTKO打开服务器上的文件
- vue项目引入阿里巴巴矢量图标库图标
- JS实现页面快捷键功能
- 计算机英语ppt答辩,急求英语答辩PPT范文
- FileZilla Server源码分析--大漠落日5节
- TB6612FNG模块使用说明
- composer的原理和正确使用(最新)
- 内部邮件管理的数据库设计和dwz的批量处理
- wpt eclipse 使用指南
- 辗转相除法和更相减损法原理和算法
- 可以动态控制左右滑动的ViewPager, 禁止ViewPager左右滑动