Код: Выделить всё
time
temperature_2m
precipitation_probability
precipitation
weather_code
когда я попробую
Код: Выделить всё
print(hour)["time"]
Код: Выделить всё
Traceback (most recent call last):
File "/home/bthuy/briefing/weather.py", line 40, in
print(hour["time"])
~~~~^^^^^^^^
TypeError: string indices must be integers, not 'str'
Код: Выделить всё
import requests
import json
from datetime import datetime
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": 37.897,
"longitude": -91.129,
"hourly": ["temperature_2m", "precipitation_probability", "precipitation", "weather_code"],
"temperature_unit": "fahrenheit",
"wind_speed_unit": "mph",
"precipitation_unit": "inch",
"forecast_days": 1
}
response = requests.get(url, params)
body_dict = response.json()
#print (body_dict)
#pretty_json = json.dumps(body_dict, indent=4)
#print (pretty_json)
hourly = body_dict["hourly"]
for hour in hourly:
print (hour)
{'latitude': 37.89382, 'longitude': -91.12241, 'generationtime_ms': 0.06103515625, ' utc_offset_секунды': -21600, 'часовой пояс': 'Америка/Чикаго', «timezone_abbreviation»: «CST», «высота»: 191,0, «hourly_units»: {»time»: «iso8601», «temperature_2m»: «°F», «precipitation_probability»: «%», «осадки»: «дюймы» ', 'weather_code': 'код ВМО'}, 'почасово': {'время': ['2024-11-22T00:00', '2024-11-22T01:00', '2024-11-22T02:00', '2024-11-22T03:00', '2024-11-22T04:00' , '2024-11-22T05:00', '2024-11-22T06:00', '2024-11-22T07:00', '2024-11-22T08:00', '2024-11-22T09:00', '2024-11-22T10:00', '2024-11-22T11:00', '2024-11-22T12:00', '2024-11-22T13:00', '2024-11-22T14:00', '2024-11-22T15:00', '2024-11-22T16:00', '2024-11-22T17:00', '2024-11-22T18:00', '2024-11-22T19:00', '2024-11-22T20:00', '2024-11-22T21:00', '2024-11-22T22:00', '2024-11-22T23:00'], 'температура_2м': [50,5, 49,8, 47,9, 46,6, 45,9, 45,3, 43,6, 43,2, 45,3, 50,0, 54,6, 57,7, 61,7, 63,9, 65,7, 67,1, 66,3, 62,7, 57,7, 54,6, 52,5, 51,4, 50,4, 49,2], 'вероятность_осаждения': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'осадки': [0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0.0, 0.0, 0.0], 'код_погоды': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2]}}
Подробнее здесь: https://stackoverflow.com/questions/792 ... -in-python
Мобильная версия