简体   繁体   中英

python KeyError:0

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.

Now that you've added more realistic code to your question, it seems obvious where the problem lies. The Twython streamer doesn't always send coordinate data, and it can be None - but when it does send it, the lat/long values may be nested two layers deep .

So the data structure is this:

{
    'coordinates': {
        'coordinates': [-73.971836, 40.798598],
        'type': 'Point'
    },
    ...
}

Which means your code needs to look like this:

class MyStreamer(TwythonStreamer):
    def on_success(self, data):
        if 'text' in data:
            if 'place' in data and data['place'] is not None:
                if 'coordinates' in data and data['coordinates'] is not None:
                    longitude, latitude = data['coordinates']['coordinates']

Or more simply:

class MyStreamer(TwythonStreamer):
    def on_success(self, data):
        if 'text' in data:
            place = data.get('place')
            if place is not None:
                coords = data.get('coordinates')
                if coords is not None:
                    longitude, latitude = coords['coordinates']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM