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

首頁 > 編程 > Python > 正文

python 把數據 json格式輸出的實例代碼

2019-11-25 16:30:32
字體:
來源:轉載
供稿:網友

有個要求需要在python的標準輸出時候顯示json格式數據,如果縮進顯示查看數據效果會很好,這里使用json的包會有很多操作

import json date = {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) # 排序并且縮進兩個字符輸出

 這樣就會得到如下的輸出:

{ "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"  } ]}

可以看到都已經格式化了。

這是在python中,如果直接使用命令行,希望直接轉換,可以使用 data | python -mjson.tool 來輸出json格式的數據

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

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

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

就會得到對于的value了。

以上就是小編為大家帶來的python 把數據 json格式輸出的實例代碼全部內容了,希望大家多多支持武林網~

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苍南县| 博爱县| 定陶县| 长岭县| 攀枝花市| 平顺县| 嘉荫县| 西乡县| 洛阳市| 呼玛县| 正阳县| 汤阴县| 壶关县| 漾濞| 镇安县| 福泉市| 五华县| 铜鼓县| 房山区| 泸州市| 高邮市| 台东市| 南充市| 平谷区| 泗水县| 尼木县| 互助| 塔城市| 宜川县| 勃利县| 万荣县| 大荔县| 格尔木市| 贡嘎县| 姚安县| 安仁县| 子长县| 泽州县| 汉阴县| 泸州市| 山东省|