gpt4 book ai didi

Python 3 日历

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:38 24 4
gpt4 key购买 nike

有人知道如何让Python从日历函数返回这样的日期格式吗?:

date = "20170714"

基本上,如果 API 请求可以收集每小时的天气数据,我正在寻找一些增强功能。我的问题是每天都需要重新运行代码。重新运行有点笨重且耗时编写 365 次脚本来获取一年的天气数据,但这是我知道的唯一方法。带有日历功能的循环可以自动化操作吗?

from urllib.request import urlopen
from pandas.io.json import json_normalize
import json
import pandas as pd

api_key = ""
date = "20170714"
zip_code = "96345"

response = urlopen("http://api.wunderground.com/api/%s/history_%s/q/%s.json" % (api_key, date, zip_code))

json_data = response.read().decode('utf-8', 'replace')

data = json.loads(json_data)

for observation in data['history']['observations']:
print("Date/Time: " + observation['date']['pretty'])
print("Temperature: " + observation['tempi'])
print("Humidity: " + observation['hum'])

df = json_normalize(data['history']['observations'])

df = df[['date.pretty','tempi','hum']]
df['date.pretty'] = pd.to_datetime(df['date.pretty'])

print(df)

这个简单的日历脚本可以返回 2013 年 12 月的每一天,但它不是地下天气 API 可以理解的格式...最终我希望创建一个循环,其中 API 请求可以收集一个月的数据,(甚至一次一整年)日历功能与一日手动时尚......这可能吗?!

import calendar

tc = calendar.TextCalendar(firstweekday=0)

for i in tc.itermonthdays(2013,12):
print(i)

谢谢

最佳答案

使用免费 API key 每天可以发出的请求数量是有限制的,但这是我建议的方式。请注意,任何与日期和/或时间相关的内容我几乎总是使用箭头包。按照这些思路的东西应该对你有用。

import arrow # learn more: https://python.org/pypi/arrow
from WunderWeather import weather # learn more: https://python.org/pypi/WunderWeather

api_key = ''
extractor = weather.Extract(api_key)
zip = '96345'

# get 20170101 00:00
begin_date = arrow.get("2017","YYYY")
# get 20171231 23:00
end_date = arrow.get("2018","YYYY").shift(hours=-1)
for date in arrow.Arrow.range('hour',begin_date,end_date):
# get date object for feature
# http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.weather.Extract.date
date_weather = extractor.date(zip,date.format('YYYYMMDD'))

# use shortcut to get observations and data
# http://wunderweather.readthedocs.io/en/latest/WunderWeather.html#WunderWeather.date.Observation
for observation in date_weather.observations:
print("Date:",observation.date_pretty)
print("Temp:",observation.temp_f)

关于Python 3 日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47596202/

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