2

我有以下代码过滤特定位置跨度,但我真的希望它只返回具有位置坐标的推文。目前,如果没有,它将返回“无”,但我想完全忽略它们。有任何想法吗?

  loc = [-3.72,50.29,-3.42,50.41]

class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):

        try:
            print "%s\t%s\t%s\t%s\t%s" % (status.text,
                                      status.author.screen_name,
                                      status.created_at,
                                      status.source,
                                      status.coordinates)          

        except Exception, e:
            print >> sys.stderr, 'Encountered Exception:', e
            pass

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)

streaming_api.filter(follow=None, locations=loc)
4

1 回答 1

0

如果您只需要从列表中删除 None 的元素,您可以这样做:

>>> l = ['a', 'b', None, 'c', None]
>>> filter(None, l)
['a', 'b', 'c']
于 2012-08-21T10:34:25.967 回答