今天 在执行下面这段代码的时候,
#!/usr/bin/env python # # ______ _ _ ______ _ ______ _ #(_____ | | | | (______) | | / _____) | # _____) )__ _ _ _ _____ ____ _____ __| | | |__ _ _ _ _ _____ ____| | _ ( (____ | | _ _ _ #| ____/ _ | | | | ___ |/ ___) ___ |/ _ | | _ | | | | | | | (____ |/ ___) |_/ ) ____ | |_/ ) | | | #| | | |_| | | | | ____| | | ____( (_| | | |_) ) |_| | | |__/ // ___ | | | _ ( _____) ) _ (| |_| | #|_| ___/ ___/|_____)_| |_____)____| |____/ __ | |_____/ _____|_| |_| _) (______/|_| _)__ | # (____/ (____/ # A python program which uses publically available # web apis to find out what the weather is. # # You'll need an API key below... you can get 1000 requests per day for free. # Go to https://darksky.net/dev/ and sign up. API="Your key" URL="https://api.darksky.net/forecast/" # Change the below for your latitude and longitude, Below figures are for the centre of Brussels, Belgium. LAT= 50.8467 LNG= 4.3526 # Change the below for different measurement units, values are : # auto = automatically select based upon geographical region # ca = same as si, except windSpeed is in kilometers per hour # uk2 = same as si, except that nearestStormDistance and visibility are in miles and windSpeed is in miles per hour # us = Imperial units # si : SI units UTS="si" directions = ["N", "NNE", "ENE", "E", "ESE", "SSE", "S", "SSW", "WSW", "W", "WNW", "NNW"] def bearing_to_direction(bearing): d = 360. / 12. return directions[int((bearing+d/2)/d)] import sys import os import time import optparse import json import urllib2 now = time.time() cached = False if os.path.exists("WEATHER.cache"): f = open("WEATHER.cache") parsed = json.loads(f.read()) f.close() if now - parsed["currently"]["time"] < 900: cached = True if cached: print "::: Using cached data..." else: print "::: Reloading cache..." req = urllib2.Request(URL+API+"/"+("%f,%f"%(LAT,LNG)+"?units="+UTS)) response = urllib2.urlopen(req) parsed = json.loads(response.read()) f = open("WEATHER.cache", "w") f.write(json.dumps(parsed, indent=4, sort_keys=True)) f.close() ; c = parsed["currently"] print ":::", time.strftime("%F %T", time.localtime(c["time"])) print "::: Conditions:", c["summary"] print "::: Temperature:", ("%.1f" % c["temperature"])+u"u00B0" print "::: Feels Like:", ("%.1f" % c["apparentTemperature"])+u"u00B0" print "::: Dew Point:", ("%.1f" % c["dewPoint"])+u"u00B0" print "::: Humidity:", ("%4.1f%%" % (c["humidity"]*100.)) print "::: Wind:", int(round(c["windSpeed"])), "mph", bearing_to_direction(c["windBearing"]) d = parsed["daily"]["data"][0] print "::: High:", ("%.1f" % d["temperatureMax"])+u"u00B0" print "::: Low:", ("%.1f" % d["temperatureMin"])+u"u00B0" d = parsed["hourly"]["data"] for x in d[:12]: print time.strftime("t%H:%M", time.localtime(x["time"])), x["summary"], ("%.1f" % x["temperature"])+u"u00B0"
出现了如下的错误信息:
Traceback (most recent call last):
File "C:/Users/My/tt.py", line 68, in
print ":::", time.strftime("%F %T", time.localtime(c["time"]))
ValueError: Invalid format string
通过在网上查找资料,看了《Python Invalid format string》这篇文章才知道,原来是时间格式的问题,时间格式在Windwos和Linux平台是不一样的。在《time gmtime localtime strftime 时间函数》这篇文章中查到了%F、%T的含义。
%F:年-月-日
%T:显示时分秒:hh:mm:ss
于是将68行代码改成:
print ":::", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(c["time"]))
python中%符号详解
%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号