python 格式化json打印

正常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"
}

posted @ 2013-10-28 13:29  天下.无贼  阅读(1223)  评论(0)    收藏  举报