比如原始的List变量的值是这种:

 [{"yearMonth": {"month": {"string": "November", "value": ""}, "year": {"string": "", "value": ""}}, "reservedMonthList": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]}, {"yearMonth": {"month": {"string": "December", "value": ""}, "year": {"string": "", "value": ""}}, "reservedMonthList": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]}]

而想要将其输出为带缩进的,树状的,很漂亮的效果,那么可以通过这样的方法:

 import json

 #demoDictList is the value we want format to output
jsonDumpsIndentStr = json.dumps(demoDictList, indent=1)
print "jsonDumpsIndentStr=",jsonDumpsIndentStr

输出:

 [
{
"yearMonth": {
"month": {
"string": "November",
"value": ""
},
"year": {
"string": "",
"value": ""
}
},
"reservedMonthList": [
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
},
{
"yearMonth": {
"month": {
"string": "December",
"value": ""
},
"year": {
"string": "",
"value": ""
}
},
"reservedMonthList": [
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
}
]

传递给json.dumps时,没有添加indent=1的话:

 import json

 #demoDictList is the value we want format to output
jsonDumpsIndentStr = json.dumps(demoDictList)
print "jsonDumpsIndentStr=",jsonDumpsIndentStr

则就是输出的,前面已经给出的,紧凑型的,没有缩进和换行的,原始的JSON字符串了:

 [{"yearMonth": {"month": {"string": "November", "value": ""}, "year": {"string": "", "value": ""}}, "reservedMonthList": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]}, {"yearMonth": {"month": {"string": "December", "value": ""}, "year": {"string": "", "value": ""}}, "reservedMonthList": ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]}]

Python中将(字典,列表等)变量格式化成字符串输出的更多相关文章

  1. Python中将字典转为成员变量

    技术背景 当我们在Python中写一个class时,如果有一部分的成员变量需要用一个字典来命名和赋值,此时应该如何操作呢?这个场景最常见于从一个文件(比如json.npz之类的文件)中读取字典变量到内 ...

  2. python中将函数赋值给变量时需要注意的一些问题

    python中将函数赋值给变量时需要注意的一些问题 变量赋值是我们在日常开发中经常会遇到的一个问题,本文主要给大家介绍的是关于python将函数赋值给变量时需要注意的一些问题,分享出来供大家参考学习, ...

  3. python将字典列表导出为Excel文件的方法

    将如下的字典列表内容导出为Excel表格文件形式: ​ 关于上图字典列表的写入,请参考文章:https://blog.csdn.net/weixin_39082390/article/details/ ...

  4. NSDateFormatter 今年日期格式化成字符串是明年日期问题?

    在项目里我要是把NSDate格式化成字符串 我的format是@"YYYY年MM月dd日 HH:mm" 传入日期2013-12-30 15:00:00后,返回给我的字符串是 201 ...

  5. python空字典列表两种生成方式对赋值带来的不同影响

    最近在实现朴素贝叶斯算法碰到一个很有趣的现象(基于Python3.6.3) 我生成一个由10个空字典构成的列表: 第一种生成方式(可能大多数人都会用这种方法,形式简单) >>a = [{} ...

  6. Sql Server中日期时间格式化为字符串输出

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  7. Python中将字典转换为有序列表、无序列表的方法

    说明:列表不可以转换为字典 1.转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a.keys()) ...

  8. python中将字典形式的数据循环插入Excel

    1.我们看到字典形式的数据如下所示 list=[["2891-1", "D"],["2892-1", "D"],[&qu ...

  9. Python基础(列表中变量与内存关系)

    在Python中,copy的是内存地址,引用的是列表的引用地址,列表里存的是各个元素的地址 例如: name = [1,2,3,4,['xfxing','summer',6]] n2 = name.c ...

随机推荐

  1. Centos7添加密码安全策略

    设置密码中至少包含一个小写字符,执行命令:# authconfig --enablereqlower --update查看设置:# grep "^lcredit" /etc/sec ...

  2. Java常见Exception类型及中文翻译

    地址:http://rymden.nu/exceptions.html 翻译: java.lang ArithmeticException 你正在试图使用电脑解决一个自己解决不了的数学问题,请重新阅读 ...

  3. D - Yet Another Problem On a Subsequence CodeForces - 1000D (DP,组合数学)

    D - Yet Another Problem On a Subsequence CodeForces - 1000D The sequence of integers a1,a2,-,aka1,a2 ...

  4. 返回的json数据中有属性为null的情况,报错 "message" : "Could not write JSON: Object is null

  5. 关于HTML5视频标签的问题

    一.基本 video标签在兼容性上还是比较差的,如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theora或者VP8的(Opera.Mozilla.Chrome),支持H.264的( ...

  6. mysql random 字母大小写和数字

    delimiter $$drop function if exists rand_string;create function rand_string(n int) returns varchar(2 ...

  7. nginx第七天

    nginx的proxy_buffering和proxy_cache 两个都是nginx代理中内存设置相关的参数. proxy_buffering设置 proxy_buffering主要是实现被代理服务 ...

  8. docker下安装运行mysql的过程以mysql5.7为例

    一.查找mysql资源 docker search mysql 其实这步顶多是看看有哪些mysql资源,除非你自己commit过一个特定的版本,否则直接执行下一步 二.安装mysql docker p ...

  9. Python之文字转图片

    Pygame模块一览表: 引入pygame模块 ,若本机没有请自行pip install pygame #载入必要的模块 import pygame #pygame初始化 pygame.init() ...

  10. Acwing-164-可达性统计(拓扑排序, 位运算统计)

    链接: https://www.acwing.com/problem/content/166/ 题意: 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量. 思路: 先拓扑排序求 ...