My program streaming data from Twython generates this error:
longitude=data['coordinates'][0]
KeyError: 0
This occurs in the following code:
class MyStreamer(TwythonStreamer):
def on_success(self, data):
if 'text' in data:
if data['place']!=None:
if 'coordinates' in data and data['coordinates'] is not None:
longitude=data['coordinates'][0]
I then inserted a print(data['coordinates'])
the line before the longitude statement and the most recent time this error intermittently happened it printed out {'coordinates': [-73.971836, 40.798598], 'type': 'Point'}
. Though sometimes it reverses the order of key entries like this:
{'type': 'Point', 'coordinates': [-73.97189946, 40.79853829]}
I then added print
calls for type(data)
and type(data['coordinates'])
and got dict
as the result for both when the error happened.
I also now realize this has only happened (and happens every time) when data['place']!=None
.
So I am now doing print calls on data['place']
,type(data['place'])
and repr(data['place'])
What else can I put in here to trap for the error/figure out what is going on?
If it helps here is the 200 line python file that includes the TwythonStreamer class definition.