gpt4 book ai didi

python-3.x - Dark Sky API 在 Python 3 中每天迭代一年

转载 作者:行者123 更新时间:2023-12-02 03:02:59 27 4
gpt4 key购买 nike

我只是尝试获取某个时间范围内的天气数据。我想获取一整年的每日或每小时数据。我刚刚尝试了以下代码:

from forecastiopy import *
from datetime import date, datetime, timedelta

def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + timedelta(n)

start_date = date(2015, 1, 1)
end_date = date(2015, 12, 31)
for single_date in daterange(start_date, end_date):
time = single_date.strftime("%Y-%m-%d")
print('DATE: ', time)


city = [40.730610, -73.935242]

fio = ForecastIO.ForecastIO(apikey,
units=ForecastIO.ForecastIO.UNITS_SI,
lang=ForecastIO.ForecastIO.LANG_ENGLISH,
latitude=city[0], longitude=city[1])

print('Latitude:', fio.latitude, 'Longitude:', fio.longitude)
print('Timezone', fio.timezone, 'Offset', fio.offset)
print(fio.get_url()) # You might want to see the request url

if fio.has_hourly() is True:
hourly = FIOHourly.FIOHourly(fio)
print('Hourly')
print('Summary:', hourly.summary)
print('Icon:', hourly.icon)

for hour in range(0, hourly.hours()):
print('Hour', hour+1)
for item in hourly.get_hour(hour).keys():
print(item + ' : ' + str(hourly.get_hour(hour)[item]))
# Or access attributes directly for a given minute.
print(hourly.hour_5_time)
else:
print('No Hourly data')

我得到:

DATUM:  2015-01-01
DATUM: 2015-01-02
DATUM: 2015-01-03
...
DATUM: 2015-12-29
DATUM: 2015-12-30
Latitude: 40.73061 Longitude: -73.935242
Timezone America/New_York Offset -4
Hourly
Summary: Light rain starting this afternoon.
Icon: rain
Hour 1
visibility : 16.09
humidity : 0.52
...
Hour 49
visibility : 16.09
humidity : 0.57
apparentTemperature : 23.52
icon : partly-cloudy-day
precipProbability : 0
windGust : 2.7
uvIndex : 2
time : 1498395600
precipIntensity : 0
windSpeed : 2.07
pressure : 1014.84
summary : Mostly Cloudy
windBearing : 37
temperature : 23.34
ozone : 308.33
cloudCover : 0.65
dewPoint : 14.43
1498237200

如何使用特定年份的每一天的时间参数来获取 365 日报告或 365 * 24 小时报告?我不是 Python 专家。

最佳答案

此博客提供了一些代码来查询日期 https://nipunbatra.github.io/blog/2013/download_weather.html 之间的代码

times = []
data = {}
for attr in attributes:
data[attr] = []

start = datetime.datetime(2015, 1, 1)
for offset in range(1, 60):
forecast = forecastio.load_forecast(api_key, lat, lng, time=start+datetime.timedelta(offset), units="us")
h = forecast.hourly()
d = h.data
for p in d:
times.append(p.time)
try:
for i in attributes:
data[i].append(p.d[i])
except:
print(KeyError)

df = pd.DataFrame(data, index=times)

它适用于 python 3.6...但是,当我查询 2019 年 3 月左右的日期以获得我的坐标时,我收到错误 KeyError: 'temperature'... 所以在这段代码中,我在 for 中添加了 try catach 错误p 在 d 循环中

希望对你有帮助

关于python-3.x - Dark Sky API 在 Python 3 中每天迭代一年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44723910/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com