想获得漂亮的格式化字符串后输出,可以使用json.dumps() 的indent 参数。它会使得输出和pprint() 函数效果类似

>>> data
{'age': 4, 'name': 'niuniuche', 'attribute': 'toy'}
>>> import json
>>> print(json.dumps(data))
{"age": 4, "name": "niuniuche", "attribute": "toy"}
>>> print(json.dumps(data,indent=3))
{
"age": 4,
"name": "niuniuche",
"attribute": "toy"
}
>>>

python 使用json.dumps() 的indent 参数,获得漂亮的格式化字符串后输出的更多相关文章

  1. python中json.dumps使用的坑以及字符编码

    我们知道,python中的字符串分普通字符串和unicode字符串,一般从数据库中读取的字符串会自动被转换为unicode字符串 下面回到重点,使用json.dumps时,一般的用法为: >&g ...

  2. python的 json.dumps 中文编码

    python的 json.dumps 中文编码 # -- coding: utf-8 -- 的作用:文件内容以utf-8编码 json.dumps 序列化时对中文默认使用的ascii编码, print ...

  3. 【python】 json.dumps() json.dump()的区别

    以前写的很简单,只有几句话,最近发现本文是本博客阅读量最大的一篇文章,觉得这样有种把人骗进来的感觉,于是又细化了一些.如果还有不好的地方,欢迎指出. 首先说明基本功能: dumps是将dict转化成s ...

  4. Python之json使用

    一.概念 json是一种通用的数据类型,任何语言都认识 接口返回的数据类型都是json 长得像字典,形式也是k-v { } 其实json是字符串 字符串不能用key.value来取值,要先转成字典才可 ...

  5. python中json的基本使用

    一.json的概念 json是一种通用的数据类型 一般情况下接口返回的数据类型都是json 长得像字典,形式也是k-v{ } 其实json是字符串 字符串不能用key.value来取值,所以要先转换为 ...

  6. python json.dumps()函数输出json格式,使用indent参数对json数据格式化输出

    在python中,要输出json格式,需要对json数据进行编码,要用到函数:json.dumps json.dumps() :是对数据进行编码 #coding=gbkimport json dict ...

  7. python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码

    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...

  8. Python学习---JSON补充内容[中文编码 + dumps解析]

    JSON补充内容[微信解决中文乱码,接上] import json # 英文显示 dic = {"hello": "world"} str = json.dum ...

  9. python json.dumps() 中文乱码问题

    python json.dumps() 中文乱码问题   python 输出一串中文字符,在控制台上(控制台使用UTF-8编码)通过print 可以正常显示,但是写入到文件中之后,中文字符都输出成as ...

随机推荐

  1. Linux下Redis的安装与启动

    一. 进入目录(我们准备将redis装入opt文件夹) $ cd /opt/ 二.下载redis压缩包 $ wget http://download.redis.io/releases/redis-4 ...

  2. 【JMeter】JMeter如何输出测试报告

    环境要求 1:jmeter3.0版本之后开始支持动态生成测试报表 2:jdk版本1.7以上 3:需要jmx脚本文件 基本操作 1:在你的脚本文件路径下,执行cmd命令:jmeter -n -t tes ...

  3. centos删除用户出错userdel: user xxx is currently used by process 23750

    今天ytkah管理centos用户准备删除某个用户时出错了,提示userdel: user xxx is currently used by process 23750,这是因为xxx用户还在登陆中, ...

  4. Redis入门到高可用(三)——通用命令

    通用命令  参考 http://redisdoc.com/index.html 1. keys  #查看所有key 时间复杂度:O(N), N 为数据库中 key 的数量. 127.0.0.1:637 ...

  5. 如何利用VMware安装XP系统

    如何利用VMware安装XP系统 百度经验 http://jingyan.baidu.com/article/215817f78ba0c51eda142322.html 1    运行分区工具 2   ...

  6. java实现的加密解密

    void encode(File enfile, File defile) throws Exception { String Algorithm = "DES"; byte[] ...

  7. windows 邮槽mailslot 在服务程序内建立后客户端无权限访问(GetLastError() == 5)的问题

    邮槽创建在服务程序内,可以创建成功, 但外部客户端连接时 m_hMailslot = CreateFile("\\\\.\\mailslot\\zdpMailslot",GENER ...

  8. git push 报错:missing Change-Id in commit message footer

    使用gerrit后,提交代码会出现如下截图问题: 临时解决: step1:把上面红色的那条gitidir复制下来执行下: step2:执行下面的命令会添加change_id git commit -- ...

  9. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  10. [LeetCode] 408. Valid Word Abbreviation_Easy

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...