Tweepy Python元组错误无属性编码

Tweepy Python元组错误无属性编码,python,twitter,tuples,tweepy,twitter-streaming-api,Python,Twitter,Tuples,Tweepy,Twitter Streaming Api,我使用tweepy和python收集基于特定关键字的推文,然后将这些状态更新(推文)写入CSV文件。我不认为自己是程序员,我真的很迷茫。 以下是错误: > Traceback (most recent call last): File "./combined-tweepy.py", line 58, in <module> sapi.filter(track=[topics]) File "/usr/local/lib/python2.7/dist-packag

我使用tweepy和python收集基于特定关键字的推文,然后将这些状态更新(推文)写入CSV文件。我不认为自己是程序员,我真的很迷茫。 以下是错误:

> Traceback (most recent call last):
  File "./combined-tweepy.py", line 58, in <module>
    sapi.filter(track=[topics])
  File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 286, in filter
    encoded_track = [s.encode(encoding) for s in track]
AttributeError: 'tuple' object has no attribute 'encode'
>回溯(最近一次呼叫最后一次):
文件“/combined tweepy.py”,第58行,在
过滤器(曲目=[主题])
文件“/usr/local/lib/python2.7/dist packages/tweepy/streaming.py”,第286行,在过滤器中
encoded_track=[s.encode(编码)用于轨迹中的s]
AttributeError:“tuple”对象没有属性“encode”
以下是脚本:

#!/usr/bin/python
import sys
import re
import tweepy
import codecs
import datetime

consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

# Create a list of topics
with open('termList.txt', 'r') as f:
  topics = [line.strip() for line in f]

stamp = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S')
topicFile = open(stamp + '.csv', 'w+')
sapi = tweepy.streaming.Stream(auth, CustomStreamListener(topicFile))
sapi.filter(track=[topics])

class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, output_file, api=None):
        super(CustomStreamListener, self).__init__()
        self.num_tweets = 0
        self.output_file = output_file

    def on_status(self, status):
        ### Writes one tweet per line in the CSV file
        cleaned = status.text.replace('\'','').replace('&amp;','').replace('&gt;','').replace(',','').replace("\n",'')
        self.num_tweets = self.num_tweets + 1
        if self.num_tweets < 500:
            self.output_file.write(status.user.location.encode("UTF-8") + ',' + cleaned.encode("UTF-8") + "\n")
            print ("capturing tweet from list")
            # print status.user.location
            return True
        else:
            return False
            sys.exit("terminating")

    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

f.close()
#/usr/bin/python
导入系统
进口稀土
进口粗花呢
导入编解码器
导入日期时间
消费者密钥=“”
消费者_secret=“”
access_key=“”
access_secret=“”
auth=tweepy.OAuthHandler(使用者密钥,使用者密钥)
授权设置访问令牌(访问密钥、访问密钥)
api=tweepy.api(auth)
#创建主题列表
以open('termList.txt','r')作为f:
topics=[line.strip()表示f中的行]
stamp=datetime.datetime.now().strftime(“%Y-%m-%d-%H%m%S”)
topicFile=open(戳记+'.csv',w+')
sapi=tweepy.streaming.Stream(auth,CustomStreamListener(topicFile))
过滤器(曲目=[主题])
类CustomStreamListener(tweepy.StreamListener):
def uuu init uuu(self,output_file,api=None):
超级(CustomStreamListener,self)。\uu初始化
self.num_tweets=0
self.output\u file=输出文件
def on_状态(自身、状态):
###在CSV文件中每行写入一条tweet
已清除=状态。文本。替换('\'','')。替换('&;'','')。替换(''','')。替换(','','')。替换('\n'','')
self.num_tweets=self.num_tweets+1
如果self.num_tweets<500:
self.output_file.write(status.user.location.encode(“UTF-8”)+,“+cleaned.encode(“UTF-8”)+“\n”)
打印(“从列表中捕获tweet”)
#打印状态.user.location
返回真值
其他:
返回错误
系统退出(“终止”)
def on_错误(自身、状态代码):
打印>>sys.stderr,“遇到状态代码错误:”,状态代码
返回True#不要杀死流
def on_超时(自身):
打印>>sys.stderr,“超时…”
返回True#不要杀死流
f、 关闭()
根据Python的文档,这里是一个示例。主题中的一个词似乎是元组

我看到了其他一些小错误。首先,按照编写代码的方式,应该在定义函数后调用它们。例如,这两条线

sapi = tweepy.streaming.Stream(auth, CustomStreamListener(topicFile))
sapi.filter(track=[topics])
应该在定义中的所有函数之后出现

class CustomStreamListener(tweepy.StreamListener):
此外,没有必要将主题放在大括号中

sapi.filter(track=[topics])
因为它已经是根据这一行的列表了

topics = [line.strip() for line in f]

你能给我们看看termList.txt的内容吗?

termList.txt中有没有一个叫做encode的东西?我不知道如何在评论中用列表的形式来表达:黑石维塞洛德·皮鲁·克里普斯·巴里奥·阿兹特卡FBD 624 BDS MLD诺特诺斯·坦戈斯·瓦卢科斯·奥雷亚斯·福里托斯·霍斯通·苏雷诺斯·特里尼塔里斯·阿曼尼亚·亚述·努埃斯特拉辛迪加·哈默斯金Lowriders Volksfront Capirucha Corpitos Tangos Mandingo Pocos Tongs Salvatrucha MS-13 Sureno 915其中一个主题是915和624,区号代表一个帮派。我在上面的评论中提到了这一点,txt文件中每行有一个条目。我认为你的主题列表的构建方式没有问题。尝试更正我以前在您的代码中发现的小错误。也许会有帮助。目前,我真的不知道还能做什么。谢谢蓝筹股,我会看看这会把我引向何方。