国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

python把數(shù)據(jù)json格式輸出

2019-11-14 16:55:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

 

 

有個(gè)要求需要在python的標(biāo)準(zhǔn)輸出時(shí)候顯示json格式數(shù)據(jù),如果縮進(jìn)顯示查看數(shù)據(jù)效果會(huì)很好,這里使用json的包會(huì)有很多操作

 

import jsondate = {u'versions': [{u'status': u'CURRENT', u'id': u'v2.3', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.2', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.1', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v2.0', u'links': [{u'href': u'http://controller:9292/v2/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v1.1', u'links': [{u'href': u'http://controller:9292/v1/', u'rel': u'self'}]}, {u'status': u'SUPPORTED', u'id': u'v1.0', u'links': [{u'href': u'http://controller:9292/v1/', u'rel': u'self'}]}]}PRint json.dumps(data, sort_keys=True, indent=2) # 排序并且縮進(jìn)兩個(gè)字符輸出

 

這樣就會(huì)得到如下的輸出:

{  "versions": [    {      "id": "v2.3",       "links": [        {          "href": "http://controller:9292/v2/",           "rel": "self"        }      ],       "status": "CURRENT"    },     {      "id": "v2.2",       "links": [        {          "href": "http://controller:9292/v2/",           "rel": "self"        }      ],       "status": "SUPPORTED"    },     {      "id": "v2.1",       "links": [        {          "href": "http://controller:9292/v2/",           "rel": "self"        }      ],       "status": "SUPPORTED"    },     {      "id": "v2.0",       "links": [        {          "href": "http://controller:9292/v2/",           "rel": "self"        }      ],       "status": "SUPPORTED"    },     {      "id": "v1.1",       "links": [        {          "href": "http://controller:9292/v1/",           "rel": "self"        }      ],       "status": "SUPPORTED"    },     {      "id": "v1.0",       "links": [        {          "href": "http://controller:9292/v1/",           "rel": "self"        }      ],       "status": "SUPPORTED"    }  ]}

 可以看到都已經(jīng)格式化了。

 

 

這是在python中,如果直接使用命令行,希望直接轉(zhuǎn)換,可以使用 data | python -mjson.tool 來(lái)輸出json格式的數(shù)據(jù)

echo '{"first_key": "value", "second_key": "value2"}' | python -mjson.tool 

 

比如想直接在命令行中過(guò)濾得到first_key對(duì)于的值,那么這樣即可:

echo '{"first_key": "value", "second_key": "value2"}' | python -c 'import sys, json; print json.load(sys.stdin)[sys.argv[1]]' first_key

就會(huì)得到對(duì)于的value了。

 

 

參考資料:

 

http://liuzhijun.VEvb.com/blog/1859857

http://docs.python.org/2/library/json.html

http://m.survivalescaperooms.com/coser/archive/2011/12/14/2287739.html
http://pymotw.com/2/json/


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿克陶县| 上饶县| 黄骅市| 常山县| 白山市| 股票| 河间市| 灯塔市| 时尚| 隆安县| 荔浦县| 秭归县| 乌鲁木齐县| 安宁市| 和林格尔县| 衡南县| 麻栗坡县| 盖州市| 青海省| SHOW| 稻城县| 大埔县| 涡阳县| 循化| 息烽县| 东港市| 衡南县| 仲巴县| 黑山县| 沂源县| 三原县| 太谷县| 盐边县| 山丹县| 东乌| 陇西县| 闸北区| 阜城县| 江达县| 白水县| 白沙|