How python traverses the weekly temperature of a specified city

Source: Internet
Author: User
This article describes how to use python to traverse the weekly temperature of a specified city. It has good reference value. let's take a look at it below. This article mainly introduces how to use python to traverse the weekly temperature of a specified city. It has good reference value. let's take a look at it together with the small editor.

If you are interested, write a weather forecast that traverses the specified city for five days and convert it to Fahrenheit.

Write the city name to a list so that you can easily add a city. With detailed notes

Import requestsimport json # define a function to avoid code rewriting multiple times. Def gettemp (week, d_or_n, date ): wendu = data ['result'] ['weate'] [week] ['info'] [d_or_n] [date] # Split the dictionary return int (wendu) def getft (t): ft = t * 1.8 + 32 return float (str (ft) [0: 4]) cities = ['baoding ', 'Beijing', 'Shanghai ', 'Wuhan ', 'zhengzhou', 'qiqihar '] # you can specify the url of the city to be traversed here =' http://api.avatardata.cn/Weather/Query?key=68e75677978441f6872c1106175b8673&cityname= '# String concatenation with the city in cities low = 0 high = 2for city in cities: r = requests. get (url + city) # Basic GET request # print (r. status_code) get the returned status 200 is successful # print (r. text) print the decoded returned data = json. loads (r. text) # The returned json data is converted to the dictionary type # print (type (data) data is of the dictionary type, so you can operate according to the dictionary (the list in the dictionary is operated by the list) print (city, 'weather forecast for the last five days: ') for I in range (5 ): week = 'week' + str (data ['result'] ['weate'] [I] ['week']) # Split dictionary types one by one, such as list tuples. Daylow = gettemp (I, 'day', low) dlf = getft (daylow) dayhigh = gettemp (I, 'day', high) dhf = getft (dayhigh) nightlow = gettemp (I, 'Night ', low) nlf = getft (nightlow) nighthweigh = gettemp (I, 'Night', high) nhf = getft (nighthweigh) print (week, 'daytime temperature: ', daylow ,'~ ', Dayhigh, 'degree Celsius', 'Evening temperature: ', nightlow ,'~ ', Niger, 'degree Celsius') print ('', 'Daytime temperature: ', dlf ,'~ ', Dhf, 'Fahrenheit', 'Evening temperature: ', nlf ,'~ ', Nhf, 'Fahrenheit') print ('\ n') {"result": {"realtime": {"wind": {"windspeed": null, "direct": "West Wind", "power": "Level 3", "offset": null}, "time": "16:00:00", "weather ": {"humidity": "27", "img": "0", "info": "Clear", "temperature": "13"}, "dataUptime ": "1490517362", "date": "", "city_code": "101090201", "city_name": "Baoding", "week": "0", "moon ": "August 9"}, "life": {"date": "2017-3-26", "info": {"kongtiao": ["enable heating and air conditioning ", "You will feel a little cold. you can turn on the heating air conditioner to adjust the indoor temperature to avoid catching a cold.. "]," Yundong ": [" more suitable "," the weather is good, but considering the strong wind power and low temperature, we recommend that you exercise indoors, if you are outdoors, pay attention to wind and increase or decrease clothing. "]," Ziwaixian ": [" moderate "," moderate intensity of ultraviolet radiation weather, when going out, it is recommended to wipe sunscreen products with SPF higher than 15, PA +, wearing a hat, sunglasses. "]," Ganmao ": [" easy to use "," large temperature difference between day and night, easy to catch a cold, Please increase or decrease clothes. If you are physically weak, pay attention to protection. "]," Xiche ": [" more suitable "," more suitable for car washing, no rain in the next day, less wind power, scrubbed a new car can be maintained for at least one day. "]," Wuran ": null," chuanyi ": [" cold "," the weather is cold. we recommend winter clothes such as cotton clothing, down jackets, leather jackets, and wool sweaters. Old and weak people should wear thick cotton coats, winter coats or thick down jackets. "] }}," Weather ": [{" date ":" February "," week ":" Day "," nongli ":" August 9 ", "info": {"dawn": null, "day": ["0", "Qing", "17", "Northwest Wind", "3-4 ", ""], "night": ["0", "Sunny", "2", "Southwest China", "Breeze", ""]}, {"date": "February 30", "week": "1", "nongli": "", "info": {"dawn": ["0 ", "Qing", "2", "Southwest China", "Breeze", ""], "day": ["0", "Qing", "15 ", "Nanfeng", "Breeze", ""], "night": ["7", "light rain", "3", "Nanfeng", "Breeze ", ""] },{ "date": "March", "week": "Two", "nongli": "January 1,", "info ": {" Dawn ": [" 7 "," light rain "," 3 "," Nanfeng "," Breeze "," "]," day ": ["1", "Cloudy", "15", "Nanfeng", "Breeze", ""], "night": ["0", "clear ", "3", "Nanfeng", "Breeze", ""] },{ "date": "", "week": "3", "nongli ": "", "info": {"dawn": ["0", "Qing", "3", "Nanfeng", "Breeze", ""], "day": ["0", "Qing", "18", "Nanfeng", "Breeze", ""], "night": ["0 ", "Qing", "3", "Bei Feng", "Breeze", "18:39"] },{ "date": "", "week": "4 ", "nongli": "", "info": {"dawn": ["0", "Qing", "3", "Bei Feng", "Breeze ", "18:39"], "day ": [" 0 "," clear "," 17 "," North Wind "," Breeze "," "]," night ": [" 0 ", "Qing", "3", "Bei Feng", "Breeze", ""]}], "pm25": {"key": "Baoding ", "show_desc": "0", "pm25": {"curPm": "34", "pm25": "14", "pm10": "26 ", "level": "1", "quality": "excellent", "des": "Very good air, can go out for activities"}, "dateTime": "March 26, 2017 ", "cityName": "Baoding"}, "isForeign": 0}, "error_code": 0, "reason": "Succes"} This is a returned json data, you can use the json formatting tool to view the details. loads are actually nested in the dictionary list, and the data to be retrieved is in the "result" in the dictionary, while data ['result '] Is also a dictionary, {'Life': {'date': '2017-3-26', 'info': {'yunong': ['is more appropriate ', 'The weather is good, but considering the strong wind power and the low temperature, we recommend that you exercise indoors. if you exercise outdoors, pay attention to wind and increase or decrease clothing. '], 'Xiche': ['more suitable', 'more suitable for car washing, no rain in the next day, less wind power, scrubbed a new car can be maintained for at least one day. '], 'Ganmao': ['easy-to-use ', 'Big temperature difference between day and night, easy to catch a cold, Please increase or decrease your clothes as appropriate. If you are physically weak, pay attention to protection. '], 'Ziwaixian': ['moderate ',' refers to moderate-intensity ultraviolet radiation weather. when going out, it is recommended to wipe sunscreen products with SPF higher than 15, PA +, wearing hats and sunglasses. '], 'Chuanyi': ['leng Leng', 'Weather is cold. we recommend that you wear winter clothes such as cotton clothing, down jackets, leather jackets, and sweaters. Old and weak people should wear thick cotton coats, winter coats or thick down jackets. '], 'Wuran': None, 'kongtiao ': ['turn on the heating airline',' you will feel a bit cold. you can enable the heating air conditioner to adjust the indoor temperature to avoid catching a cold. '] }}, 'Weate': [{'date': '2017-03-26', 'week': 'day', 'info': {'Dawn ': None, 'Night ': ['0', 'Qing', '2', 'Southwest wind', 'breeze', '18: 36'], 'Day ': ['0', 'Qing', '17', 'Northwest wind', '3-4 level', '06: 12']}, 'nongli ': 'August 9'}, {'date': '2017-03-27 ', 'week': '1', 'info': {'Dawn': ['0 ', 'clear', '2', 'Southwest wind', 'breeze ', '18: 36'], 'Night': ['7', 'light rain', '3 ', 'southwind ', 'breeze', '18: 37'], 'Day': ['0', 'Qing', '15', 'southwind ', 'breeze ', '06: 11']}, 'nongli': 'August 11 '}, {'Date': '2017-03-28 ', 'week': '2', 'info': {'Dawn': ['7', 'Light rain ', '3', 'southwind ', 'breeze', '18: 37'], 'Night ': ['0', 'clear', '3', 'southwind ', 'breeze ', '18: 38'], 'Day': ['1', 'cloudy', '15', 'southwind ', 'breeze', '06: 09 ']}, 'nongli': 'First day of October 1'}, {'date': '2017-03-29', 'week': '3', 'info ': {'Dawn ': ['0', 'clear', '3', 'southwind', 'breeze ', '18: 38'], 'Night ': ['0', 'Qing', '3', 'northwind ', 'breeze', '18: 39 '], 'Day': ['0 ', 'clear', '18', 'southwind ', 'breeze', '06: 08']}, 'nongli ': 'October 2's Day}, {'date': '2017-03-30 ', 'week': '4', 'info': {'Dawn': ['0 ', 'Qing', '3', 'northwind ', 'breeze', '18: 39 '], 'Night': ['0', 'Qing', '3 ', 'north wind', 'breeze ', '18: 40'], 'Day': ['0', 'Qing', '17', 'north wind', 'breeze ', '06: 06']}, 'nongli': 'September 303'}], 'isforn n': 0, 'pm25': {'pm25': {'Des ': 'air is good. you can go out for the activities', 'curps': '34', 'level': '1', 'pm10 ': '26', 'pm25 ': '14', 'Quality': 'out'}, 'show _ desc': '0', 'key': 'bading', 'datetime': 'August 0 'Cityname': 'baoding '}, 'realtime': {'City _ name': 'baoding', 'weate': {'info ': 'Qing', 'IMG ': '0', 'humidity': '27', 'tempature ': '13'}, 'Week': '0 ', 'Wind ': {'windspeed': None, 'Power': 'Level 3', 'offset': None, 'direct': 'West '}, 'City _ Code': '20170101', 'Date': '2017-03-26 ', 'datauptime': '20170101', 'Time': '16: 00: 00 ', 'Moon ': 'August 9'} The same method to retrieve data ['result'] ['weather']. this is also a tuples, [{'nongli ': 'September 29 ', 'Info': {'Night ': ['0', 'qingqing', '2', 'Southwest wind', 'breeze ', '18: 36'], 'Dawn ': None, 'Day': ['0', 'clear', '17', 'Northwest wind', '3-4 level', '06: 12 ']}, 'Week': 'day', 'Date': '2017-03-26 '}, {'nongi': 'August 1', 'info ': {'Night ': ['7', 'Light rain', '3', 'southwind ', 'breeze', '18: 37'], 'Dawn ': ['0', 'Qing', '2', 'Southwest Wind ', 'breeze', '18: 36'], 'Day': ['0 ', 'Qing', '15', 'southwind ', 'breeze', '06: 11']}, 'week': 'yi', 'Date ': '2014-03-27 '}, {'nongi': '1st of October 1', 'info': {'Night': ['0', 'Qing', '3', 'southwind ', 'breeze', '18: 38'], 'Dawn ': ['7 ', 'light rain', '3', 'southwind ', 'breeze', '18: 37'], 'Day': ['1', 'Cloudy, '15 ', 'southwind ', 'breeze', '06: 09']}, 'week': '2', 'Date': '2017-03-28 '}, {'nongli ': 'October 2', 'info': {'Night ': ['0', 'Qing', '3', 'northwind', 'breeze ', '18: 39 '], 'Dawn': ['0', 'clear', '3', 'southwind ', 'breeze', '18: 38'], 'Day ': ['0', 'Qing', '18', 'southwind ', 'breeze', '06: 08']}, 'Week ': '3 ', 'date': '1970-03-29 '}, {'nongli': 'August 3, 2017', 'Info': {'Night ': ['0', 'clear', '3', 'northwind', 'breeze ', '18: 40'], 'Dawn ': ['0', 'Qing', '3', 'northwind ', 'breeze', '18: 39 '], 'Day': ['0 ', 'clear', '17', 'northwind ', 'breeze', '06: 06']}, 'week': '4', 'Date ': '2017-03-30 '}] Then, take the dictionary in the tuples and split them to obtain the desired data.

The above is a detailed explanation of the method for python to traverse the weekly temperature of a specified city. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.