![](/img/trans.png)
[英]How to compute 2, 3 and 5-days accumulated rainfall from daily rainfall data in netcdf using Python
[英]Downloading Real time rainfall data from Data.gov.sg API (json) with Python
我想從此鏈接從氣象站S17
使用Python從Data.gov.sg
API(以json
格式)下載實時降雨數據,但是我的代碼拋出了:
TypeError:字符串索引必須是整數
感謝任何人都可以提供幫助! 謝謝!
碼
#!/usr/bin/env python
import requests
import json
import datetime as dt
from xlwt import Workbook
start_date = dt.datetime(2018, 8, 14, 00, 00, 00)
end_date = dt.datetime(2018, 8, 19, 00, 00, 00)
total_days = (end_date - start_date).days + 1
neadatasum = []
for day_number in range(total_days):
for day_time in range(0, 24, 86400):
current_date = (start_date + dt.timedelta(days = day_number)).date()
current_time = (start_date + dt.timedelta(hours = day_time)).time()
url = 'https://api.data.gov.sg/v1/environment/rainfall?date_time=' + str(current_date) + 'T' + \
str(current_time)
headers = {"api-key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
data = requests.get(url, headers=headers).json()
actualtime = str(current_date) + 'T' + str(current_time) + '+08:00'
print(current_date, current_time)
if not data['items'][0]:
station_id = 'S71'
value = 'Nan'
neadatasum1 = [current_date, current_time, value]
neadatasum.append(neadatasum1)
else:
datatime = data['items'][0]['timestamp']
if actualtime != datatime:
station_id = 'S71'
value = 'Nan'
neadatasum1 = [current_date, current_time, value]
neadatasum.append(neadatasum1)
else:
station_id = 'S71'
value = data['items'][0]['timestamp']['readings']['station_id', 'value']
neadatasum1 = [current_date, current_time, value]
neadatasum.append(neadatasum1)
print(neadatasum)
wb = Workbook()
sheet1 = wb.add_sheet('Rainfall 5 minutes(mm)')
sheet1.write(0, 0, 'Date')
sheet1.write(0, 1, 'Time')
sheet1.write(0, 2, 'Rainfall')
for i, j in enumerate(neadatasum):
for k, l in enumerate(j):
sheet1.write(i+1, k, l)
wb.save('Rainfall 5 minutes(mm)(14082018 to 19082018).xls')
追溯(最近一次通話):文件“ C:/Users/erilpm/AppData/Local/Programs/Python/Python36-32/Rainfall.py”,第36行,值= data ['items'] [0] [ 'timestamp'] ['readings'] ['station_id','value'] TypeError:字符串索引必須為整數
我不確定100%是否了解您希望此代碼執行的操作,但是我會盡力而為。 如果我誤解了您的問題,請發表評論!
好的,所以首先,您無法索引
data['items'][0]['timestamp']['readings']['station_id', 'value']
...如果您的data
看起來不像
data[ items , ..., ... ]
^
[ 0, 1, ... ]
^
[..., timestamp, ... ]
^
[readings, ..., ...]
^
[ (station_id, value), (...,...), ..., ... ]
……實際上,這似乎並不是您的data
看起來的樣子。 實際上,如果我執行print(data['items'][0].keys())
,我得到的只是['timestamp','readings']
。
請注意,當我打印data['items']
我得到:
[{'timestamp':'2018-08-14T00:00:00 + 08:00','readings':[{'station_id':'S77','value':0},{'station_id':'S109 ','value':0},{'station_id':'S117','value':0},{'station_id':'S55','value':0},{'station_id':'S64', 'value':0},{'station_id':'S90','value':0},依此類推等等
好的,所以我認為您想要一個類似的列表(current time, current date, some value recorded at that specific station)
。 問題是您正在查看值列表,而不是所有想要的站中的值,而不僅僅是一個值。 所以,這是我的建議。 將循環末尾的所有內容替換為以下內容:
# ok, so you like station S71, right?
station_id = 'S71'
# use list comprehension to get only values from that station
values = [a['value'] for a in data['items'][0]['readings'] if a['station_id'] == station_id]
# iterate over all the matching values
for value in values:
# and add them to your nifty list!
neadatasum1 = [current_date, current_time, value]
neadatasum.append(neadatasum1)
我希望這是有道理的! 如果這樣做不起作用,或者無法回答您的問題,或者我誤解了您的問題,請據此發表評論,我(或更有才華的人)將嘗試解決此問題,或者以其他方式回答仍有待回答的問題:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.