python(json解析)(天氣預報查詢系統)

2021-08-15 01:56:33 字數 1538 閱讀 7528

天氣預報查詢系統

#天氣預報

#引入requests

import requests

#引入python內建的包json,用來解析和生成

import json

#url統一資源定位符 主機網域名稱/目錄/?地點&格式。。。

#windows+r cmd 開啟命令列工具 輸入pip install requests

print('歡迎進入天氣預報查詢系統!')

while 1:

city = input('請輸入想要查詢的城市的名稱(輸入0退出):')

if city == '0':

print('已退出天氣預報查詢系統!')

break

else:

url = ''%city

#使用requests發起請求,接受返回的結果

response = requests.get(url)

#使用loads函式 將json字串轉化為python 的字典或列表

rs_dict = json.loads(response.text)

#取出error

error_code = rs_dict['error']

if error_code == 0:

#從字典中取出資料

results = rs_dict['results']

#根據索引取出城市天氣資訊字典

info_dict = results[0]

#取出城市名稱

city_name = info_dict['currentcity']

pm25 = info_dict['pm25']

print('當前城市:%s pm25:%s'%(city_name,pm25))

#取出天氣列表

weather_data = info_dict['weather_data']

#for迴圈取出每一天的小字典

for weather_dict in weather_data:

date = weather_dict['date']

weather = weather_dict['weather']

wind = weather_dict['wind']

temperature = weather_dict['temperature']

print('%s %s %s %s'%(date,weather,wind,temperature))

index = info_dict['index']

for index_dict in index:

title = index_dict['title']

zs = index_dict['zs']

tipt = index_dict['tipt']

des = index_dict['des']

print('%s %s %s %s'%(title,zs,tipt,des))

else:

print('沒有查詢到天氣資訊')

Python JSON 資料解析

usr bin python3 import json python 字典型別轉換為 json 物件 data json str json.dumps data print python 原始資料 repr data print json 物件 json str 執行以上 輸出結果為 python ...

Python json資料的解析

import json data jsonstr json.dumps data 編碼 print type data dict print type jsonstr str print jsonstr import json data jsonstr json.dumps data 編碼 tmp ...

影評 之 《天氣預爆》

這兩年,國內的喜劇演員也算是得到了比較不錯的發展平台,各類娛樂節目的推陳出新讓很多新的喜劇人被發掘,也有了成長了機會,通過這些喜劇節目比如 歡樂喜劇人 喜劇總動員 笑傲江湖 週六夜現場 以及 王牌對王牌 等等,認識了不少特別棒的喜劇人和喜劇團隊,不過大多還是在娛樂節目中,可能參加娛樂節目的成本和風險...