首页 > 解决方案 > 使用 Python 从 Data.gov.sg API (json) 下载实时降雨数据

问题描述

我想从这个链接的气象站使用 Python 从Data.gov.sgAPI(格式)下载实时降雨数据,但我的代码正在抛出:jsonS17

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')

Traceback(最近一次调用最后一次):文件“C:/Users/erilpm/AppData/Local/Programs/Python/Python36-32/Rainfall.py”,第 36 行,值 = data['items'][0][ 'timestamp']['readings']['station_id', 'value'] TypeError:字符串索引必须是整数

标签: pythonjson

解决方案


我不是 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', '读数': [{'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)

我希望这是有道理的!如果这不起作用,或者没有回答您的问题,或者我误解了您的问题,请相应地发表评论,我(或更有才华的人)将尝试解决此问题或以其他方式回答任何有待回答的问题:)


推荐阅读