【Python】整理列表、字典的数据格式化展示

平时 debug 的时候,经常会 print 一些列表或者字典的数据,当元素很多的时候查看起来是很乱的。这时如果可以把他们都排列好展示就最棒了。

使用 json.dumps 配合 indent 参数就可以实现了。indent 声明一次锁进的数量,非负整数。

indent = max(0, indent)

import json

print(json.dumps([1, 2, {'a': 'a'}], indent=4))
# [
#     1, 
#     2, 
#     {
#         "a": "a"
#     }
# ]

print(json.dumps({1: 1, 2: {22: 22}}, indent=4))
# {
#     "1": 1, 
#     "2": {
#         "22": 22
#     }
# }
发布了52 篇原创文章 · 获赞 143 · 访问量 29万+

猜你喜欢

转载自blog.csdn.net/lnotime/article/details/102489545
今日推荐