天天看點

Python 美化列印 json 資料

正常擷取 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": "\u676d\u5dde", 
        "city_en": "hangzhou", 
        "cityid": "101210101", 
        "date": "", 
        "date_y": "2013\u5e7410\u670812\u65e5", 
        "fchh": "11", 
        "fl1": "\u5c0f\u4e8e3\u7ea7", 
        "fl2": "\u5c0f\u4e8e3\u7ea7", 
        "fl3": "\u5c0f\u4e8e3\u7ea7", 
        "fl4": "4-5\u7ea7\u8f6c\u5c0f\u4e8e3\u7ea7", 
        "fl5": "\u5c0f\u4e8e3\u7ea7", 
        "fl6": "\u5c0f\u4e8e3\u7ea7", 
        "fx1": "\u4e1c\u5317\u98ce", 
        "fx2": "\u4e1c\u5317\u98ce", 
        "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": "\u591a\u4e91", 
        "img_title10": "\u591a\u4e91", 
        "img_title11": "\u591a\u4e91", 
        "img_title12": "\u591a\u4e91", 
        "img_title2": "\u6674", 
        "img_title3": "\u591a\u4e91", 
        "img_title4": "\u6674", 
        "img_title5": "\u591a\u4e91", 
        "img_title6": "\u9634", 
        "img_title7": "\u9635\u96e8", 
        "img_title8": "\u9634", 
        "img_title9": "\u591a\u4e91", 
        "img_title_single": "\u591a\u4e91", 
        "index": "\u70ed", 
        "index48": "\u70ed", 
        "index48_d": "\u5929\u6c14\u70ed\uff0c\u5efa\u8bae\u7740\u77ed\u88d9\u3001\u77ed\u88e4\u3001\u77ed\u8584\u5916\u5957\u3001T\u6064\u7b49\u590f\u5b63\u670d\u88c5\u3002", 
        "index48_uv": "\u4e2d\u7b49", 
        "index_ag": "\u8f83\u6613\u53d1", 
        "index_cl": "\u9002\u5b9c", 
        "index_co": "\u8f83\u8212\u9002", 
        "index_d": "\u5929\u6c14\u70ed\uff0c\u5efa\u8bae\u7740\u77ed\u88d9\u3001\u77ed\u88e4\u3001\u77ed\u8584\u5916\u5957\u3001T\u6064\u7b49\u590f\u5b63\u670d\u88c5\u3002", 
        "index_ls": "\u9002\u5b9c", 
        "index_tr": "\u9002\u5b9c", 
        "index_uv": "\u4e2d\u7b49", 
        "index_xc": "\u9002\u5b9c", 
        "st1": "29", 
        "st2": "19", 
        "st3": "29", 
        "st4": "20", 
        "st5": "29", 
        "st6": "16", 
        "temp1": "28\u2103~18\u2103", 
        "temp2": "29\u2103~19\u2103", 
        "temp3": "29\u2103~17\u2103", 
        "temp4": "19\u2103~12\u2103", 
        "temp5": "17\u2103~11\u2103", 
        "temp6": "18\u2103~12\u2103", 
        "tempF1": "82.4\u2109~64.4\u2109", 
        "tempF2": "84.2\u2109~66.2\u2109", 
        "tempF3": "84.2\u2109~62.6\u2109", 
        "tempF4": "66.2\u2109~53.6\u2109", 
        "tempF5": "62.6\u2109~51.8\u2109", 
        "tempF6": "64.4\u2109~53.6\u2109", 
        "weather1": "\u591a\u4e91\u8f6c\u6674", 
        "weather2": "\u591a\u4e91\u8f6c\u6674", 
        "weather3": "\u591a\u4e91\u8f6c\u9634", 
        "weather4": "\u9635\u96e8\u8f6c\u9634", 
        "weather5": "\u591a\u4e91", 
        "weather6": "\u591a\u4e91", 
        "week": "\u661f\u671f\u516d", 
        "wind1": "\u4e1c\u5317\u98ce\u5c0f\u4e8e3\u7ea7", 
        "wind2": "\u4e1c\u98ce\u5c0f\u4e8e3\u7ea7", 
        "wind3": "\u4e1c\u5357\u98ce\u5c0f\u4e8e3\u7ea7", 
        "wind4": "\u4e1c\u5317\u98ce4-5\u7ea7\u8f6c\u5c0f\u4e8e3\u7ea7", 
        "wind5": "\u4e1c\u98ce\u5c0f\u4e8e3\u7ea7", 
        "wind6": "\u4e1c\u98ce\u5c0f\u4e8e3\u7ea7"
    }
}           

複制