python 格式化json打印

来源:互联网 发布:信息化时代数据 编辑:程序博客网 时间:2024/05/22 12:16

正常print一个dict,显示内容是全部在一行的。如下

di = {"name":"test1", "sex":"test2", "others":[1,2,"3"]}
print di

{'others': [1, 2, '3'], 'name': 'test1', 'sex': 'test2'}

内容少,还能看得明白,多了就不明朗了,用json库能格式化

import json

di = {"name":"test1", "sex":"test2", "others":[1,2,"3"]}
print json.dumps(di, indent=4)

打印如下:

{
  "others": [
    1, 
    2, 
    "3"
  ], 
  "name": "test1", 
  "sex": "test2"
}

0 0
原创粉丝点击