Python 美化列印 json 數據

  • 2020 年 1 月 10 日
  • 筆記

正常獲取 JSON 數據,數據的顯示格式較為混亂,無法直視

可以採用瀏覽器插件方式需要安裝 Chrome 插件:JSONView

curl http://m.weather.com.cn/data/101210101.html

{"weatherinfo":{"city":"杭州","city_en":"hangzhou","date_y":"2013年10月12日","date":"","week":"星期六","fchh":"11","cityid":"101210101","temp1":"28℃~18℃","temp2":"29℃~19℃","temp3":"29℃~17℃","temp4":"19℃~12℃","temp5":"17℃~11℃","temp6":"18℃~12℃","tempF1":"82.4℉~64.4℉","tempF2":"84.2℉~66.2℉","tempF3":"84.2℉~62.6℉","tempF4":"66.2℉~53.6℉","tempF5":"62.6℉~51.8℉","tempF6":"64.4℉~53.6℉","weather1":"多雲轉晴","weather2":"多雲轉晴","weather3":"多雲轉陰","weather4":"陣雨轉陰","weather5":"多雲","weather6":"多雲","img1":"1","img2":"0","img3":"1","img4":"0","img5":"1","img6":"2","img7":"3","img8":"2","img9":"1","img10":"99","img11":"1","img12":"99","img_single":"1","img_title1":"多雲","img_title2":"晴","img_title3":"多雲","img_title4":"晴","img_title5":"多雲","img_title6":"陰","img_title7":"陣雨","img_title8":"陰","img_title9":"多雲","img_title10":"多雲","img_title11":"多雲","img_title12":"多雲","img_title_single":"多雲","wind1":"東北風小於3級","wind2":"東風小於3級","wind3":"東南風小於3級","wind4":"東北風4-5級轉小於3級","wind5":"東風小於3級","wind6":"東風小於3級","fx1":"東北風","fx2":"東北風","fl1":"小於3級","fl2":"小於3級","fl3":"小於3級","fl4":"4-5級轉小於3級","fl5":"小於3級","fl6":"小於3級","index":"熱","index_d":"天氣熱,建議著短裙、短褲、短薄外套、T恤等夏季服裝。","index48":"熱","index48_d":"天氣熱,建議著短裙、短褲、短薄外套、T恤等夏季服裝。","index_uv":"中等","index48_uv":"中等","index_xc":"適宜","index_tr":"適宜","index_co":"較舒適","st1":"29","st2":"19","st3":"29","st4":"20","st5":"29","st6":"16","index_cl":"適宜","index_ls":"適宜","index_ag":"較易發"}}

使用 python 將他們美化排版一下

curl http://m.weather.com.cn/data/101210101.html | python -mjson.tool

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                   Dload  Upload   Total   Spent    Left  Speed  100  1803    0  1803    0     0  19745      0 --:--:-- --:--:-- --:--:-- 38361  {      "weatherinfo": {          "city": "u676du5dde",          "city_en": "hangzhou",          "cityid": "101210101",          "date": "",          "date_y": "2013u5e7410u670812u65e5",          "fchh": "11",          "fl1": "u5c0fu4e8e3u7ea7",          "fl2": "u5c0fu4e8e3u7ea7",          "fl3": "u5c0fu4e8e3u7ea7",          "fl4": "4-5u7ea7u8f6cu5c0fu4e8e3u7ea7",          "fl5": "u5c0fu4e8e3u7ea7",          "fl6": "u5c0fu4e8e3u7ea7",          "fx1": "u4e1cu5317u98ce",          "fx2": "u4e1cu5317u98ce",          "img1": "1",          "img10": "99",          "img11": "1",          "img12": "99",          "img2": "0",          "img3": "1",          "img4": "0",          "img5": "1",          "img6": "2",          "img7": "3",          "img8": "2",          "img9": "1",          "img_single": "1",          "img_title1": "u591au4e91",          "img_title10": "u591au4e91",          "img_title11": "u591au4e91",          "img_title12": "u591au4e91",          "img_title2": "u6674",          "img_title3": "u591au4e91",          "img_title4": "u6674",          "img_title5": "u591au4e91",          "img_title6": "u9634",          "img_title7": "u9635u96e8",          "img_title8": "u9634",          "img_title9": "u591au4e91",          "img_title_single": "u591au4e91",          "index": "u70ed",          "index48": "u70ed",          "index48_d": "u5929u6c14u70eduff0cu5efau8baeu7740u77edu88d9u3001u77edu88e4u3001u77edu8584u5916u5957u3001Tu6064u7b49u590fu5b63u670du88c5u3002",          "index48_uv": "u4e2du7b49",          "index_ag": "u8f83u6613u53d1",          "index_cl": "u9002u5b9c",          "index_co": "u8f83u8212u9002",          "index_d": "u5929u6c14u70eduff0cu5efau8baeu7740u77edu88d9u3001u77edu88e4u3001u77edu8584u5916u5957u3001Tu6064u7b49u590fu5b63u670du88c5u3002",          "index_ls": "u9002u5b9c",          "index_tr": "u9002u5b9c",          "index_uv": "u4e2du7b49",          "index_xc": "u9002u5b9c",          "st1": "29",          "st2": "19",          "st3": "29",          "st4": "20",          "st5": "29",          "st6": "16",          "temp1": "28u2103~18u2103",          "temp2": "29u2103~19u2103",          "temp3": "29u2103~17u2103",          "temp4": "19u2103~12u2103",          "temp5": "17u2103~11u2103",          "temp6": "18u2103~12u2103",          "tempF1": "82.4u2109~64.4u2109",          "tempF2": "84.2u2109~66.2u2109",          "tempF3": "84.2u2109~62.6u2109",          "tempF4": "66.2u2109~53.6u2109",          "tempF5": "62.6u2109~51.8u2109",          "tempF6": "64.4u2109~53.6u2109",          "weather1": "u591au4e91u8f6cu6674",          "weather2": "u591au4e91u8f6cu6674",          "weather3": "u591au4e91u8f6cu9634",          "weather4": "u9635u96e8u8f6cu9634",          "weather5": "u591au4e91",          "weather6": "u591au4e91",          "week": "u661fu671fu516d",          "wind1": "u4e1cu5317u98ceu5c0fu4e8e3u7ea7",          "wind2": "u4e1cu98ceu5c0fu4e8e3u7ea7",          "wind3": "u4e1cu5357u98ceu5c0fu4e8e3u7ea7",          "wind4": "u4e1cu5317u98ce4-5u7ea7u8f6cu5c0fu4e8e3u7ea7",          "wind5": "u4e1cu98ceu5c0fu4e8e3u7ea7",          "wind6": "u4e1cu98ceu5c0fu4e8e3u7ea7"      }  }

Tags: