简体   繁体   中英

How to iterate through a list containing Twitter Stream data?

I was streaming Data from twitter using tweepy Streaming api ,

class MyStreamListener(tweepy.StreamListener):
def __init__(self):
    super().__init__()
    self.counter = 0
    self.limit = 8
    self.q=list()

def on_status(self, status):
    self.counter += 1
    if self.counter < self.limit:
        self.q.append(status.user.name)
        self.q.append(status.user.statuses_count)
        #print(status.text)
    else:
        myStream.disconnect()
        print(self.q)

In on_status method I had stored all the data and status_count in the list, but when I tried to iterate the data I wasn't able to do so

enter code here
tweet_list=list()
tweet_list.append(myStream.filter(track=[p]))
#tweet_list=myStream.filter(track=[p])
print(len(myStream.filter(track=[p])))

I get this result:

['Chanchinthar', 1063, 'Sourabh', 4424]
Traceback (most recent call last):
File "C:/Users/TharunReddy/Desktop/pothi/twitter.py", line 51, in <module>
print(len(myStream.filter(track=[p])))
TypeError: object of type 'NoneType' has no len()

How can store the tweets in the list and be able to iterate over it? someone please help!

Your myStream object is not a list but a generator (which is in turn a type of iterator). Iterators are basically iterables (like lists) whose elements are computed (lazily) one at a time: only once they are needed. This is a much more memory-efficient structure, but the length cannot be computed. (Also, you will also be able to iterate through them once).

TLDR : convert your myStream into a list: list(myStream) and everything will be fine.

For more info on generators/iterators see here

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