日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python 把數(shù)據(jù) json格式輸出的實例代碼

系統(tǒng) 1681 0

有個要求需要在python的標(biāo)準(zhǔn)輸出時候顯示json格式數(shù)據(jù),如果縮進(jìn)顯示查看數(shù)據(jù)效果會很好,這里使用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) # 排序并且縮進(jìn)兩個字符輸出
          

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

            
{
 "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 來輸出json格式的數(shù)據(jù)

            
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了。

以上就是小編為大家?guī)淼膒ython 把數(shù)據(jù) json格式輸出的實例代碼全部內(nèi)容了,希望大家多多支持腳本之家~


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 四川省| 上林县| 宜宾县| 灵川县| 浦城县| 剑阁县| 望都县| 汽车| 屏南县| 霞浦县| 通州市| 凤阳县| 扎兰屯市| 灵丘县| 当雄县| 丰宁| 岳阳县| 灵宝市| 五河县| 襄汾县| 邻水| 黔东| 南昌县| 罗平县| 涞源县| 武义县| 红原县| 万全县| 治县。| 且末县| 长宁区| 西平县| 喜德县| 九龙城区| 贵溪市| 饶河县| 磐安县| 张家川| 西吉县| 定结县| 武冈市|