python 用于将当前天气状况打印到www.DailyMile.com锻炼的TextExpander代码段。需要wunderground.com api密钥。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用于将当前天气状况打印到www.DailyMile.com锻炼的TextExpander代码段。需要wunderground.com api密钥。相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python 
#-*- coding: utf-8 -*-
"""
    Local Weather TextExpander Snippet
    ==================================
    
    Quickly enter local weather conditions, specifically current temp
    in www.dailymile.com workouts. This is useful for tracking 
    performance against temperature more closely than dailymile.com 
    currently allows with their pictures.
    
    Requires a wunderground.com api key.
"""

__author__ = Luke Thomas Mergner <lmergner@gmail.com>

import json
from datetime import datetime

import urllib2

try:
	r = urllib2.urlopen("http://api.wunderground.com/api/<API KEY>/conditions/q/CA/Glendale.json")
except URLError as e:
	if hassattr(e, 'reason'):
		print('Failure: %s' % e.reason)
	elif hassattr(e, 'code'):
		print('Error Code: %s' % e.code)

data =json.loads(r.read())


local = data['current_observation']

# fahr = "\u2109"
# When I use the unicode for Fahrenheit (℉) in the print statement, it fails.

string = u"{:%I:%M %p}: {} at {} with {} mph winds.".format(datetime.now(), local['weather'], local['temp_f'], local['wind_mph'])

print(string)

以上是关于python 用于将当前天气状况打印到www.DailyMile.com锻炼的TextExpander代码段。需要wunderground.com api密钥。的主要内容,如果未能解决你的问题,请参考以下文章

python获取天气状况并以邮件的形式发到目的邮箱

如何将 placemark.locality 放入我的天气 API?

如何从复杂的 JSON API 返回嵌套值

仅在父状态更新后如何接收道具?

App设计灵感之十二组精美的天气预报App设计案例

python语言如何使用print()打印当前行