Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何遍历包含Twitter流数据的列表?_Python_Twitter_Tweepy_Twitter Streaming Api - Fatal编程技术网

Python 如何遍历包含Twitter流数据的列表?

Python 如何遍历包含Twitter流数据的列表?,python,twitter,tweepy,twitter-streaming-api,Python,Twitter,Tweepy,Twitter Streaming Api,我使用tweepy streaming api从twitter上传输数据 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.c

我使用tweepy streaming api从twitter上传输数据

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)
我得到这个结果:

['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()
['Chanchinthar',1063',Sourabh',4424]
回溯(最近一次呼叫最后一次):
文件“C:/Users/TharunReddy/Desktop/pothi/twitter.py”,第51行,在
打印(len(myStream.filter(track=[p]))
TypeError:类型为“NoneType”的对象没有len()
如何将tweets存储在列表中并能够对其进行迭代?
有人请帮忙

您的
myStream
对象不是一个列表,而是一个生成器(它又是一种迭代器)。迭代器基本上是可重用的(如列表),其元素一次(惰性地)计算一个:只需要一次。这是一个内存效率更高的结构,但无法计算长度。(此外,您还可以对它们进行一次迭代)

TLDR:将您的
myStream
转换为列表:
list(myStream)
,一切都会好起来的

有关生成器/迭代器的更多信息,请参见

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