2017-08-25 36 views
2

Ich habe Python nur für kurze Zeit studiert, also übe ich durch die Beispiele anderer Personen. Ich möchte auf Twitter eine Wortfilterung durchführen, deren Python-Code wie folgt zusammengefasst werden kann.Objekt hat kein Attribut 'count'

import tweepy 
import simplejson as json 
from imp import reload 
import sys 

reload(sys) 

consumer_key = 'blah' 
consumer_skey = 'blah' 
access_tokenA = 'blah' 
access_stoken = 'blah' 

def get_api(): 
api_key = consumer_key 
api_secret = consumer_skey 
access_token = access_tokenA 
access_token_secret = access_stoken 
auth = tweepy.OAuthHandler(api_key, api_secret) 
auth.set_access_token(access_token, access_token_secret) 
return auth 

class CustomStreamListener(tweepy.StreamListener): 
def on_status(self, status): 
    print ('Got a Tweet') 
    self.count += 1 
    tweet = status.text 
    tweet = self.pattern.sub(' ',tweet) 
    words = tweet.split() 
    for word in words: 
     if len(word) > 2 and word != '' and word not in self.common: 
      if word not in self.all_words: 
       self.all_words[word] = 1 
      else: 
       self.all_words[word] += 1 

if __name__ == '__main__': 
l = CustomStreamListener() 
try: 
    auth = get_api() 
    s = "obamacare" 
    twitterStreaming = tweepy.Stream(auth, l) 
    twitterStreaming.filter(track=[s]) 
except KeyboardInterrupt: 
    print ('-----total tweets-----') 
    print (l.count) 
    json_data = json.dumps(l.all_words, indent=4) 
    with open('word_data.json','w') as f: 
     print >> f, json_data 
     print (s) 

Aber es gibt einen Fehler wie folgt.

File "C:/Users/ID500/Desktop/Sentiment analysis/untitled1.py", line 33, in on_status 
self.count += 1 

AttributeError: 'CustomStreamListener' object has no attribute 'count' 

Ich denke, die Version von Beispiel und meine Version ist nicht korrekt, weil ich bereits einige Teile geändert habe.

Was soll ich tun?

+0

definieren self.count = 0 innerhalb der Klasse __init__() -Methode – Kallz

Antwort

1
self.count += 1 

Python las es als

self.count = self.count + 1 

Die für self.count Suche zuerst, dann + 1 hinzufügen und zuordnen self.count.

- =, * =,/= funktioniert ähnlich für Subtraktion, Multiplikation und Division.

What += exactly do ??

in Ihrem Code Sie self.count nicht initialisiert. zu initialisieren Zählung definiert self.count in __init_() Methode der Klasse

def __init__(self) 
    self.count = 0 
+0

@RachelPark Glad wenn diese zu helfen Antwort löste Ihr Problem Bitte markieren Sie es als akzeptiert, indem Sie auf das Häkchen neben der Antwort klicken. siehe: Wie funktioniert das Annehmen einer Antwort? https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Kallz

+0

Ich überprüfte die Markierung neben der Antwort, ist es das? Weil ich diesen Stackoverflow zum ersten Mal benutze. –

+0

@RachelPark Ja, es ist richtig, die Antwort zu akzeptieren, die dein Problem löst. : :) – Kallz

0

Dies ist, weil Sie die Zählvariable entweder in Ihrer benutzerdefinierten Klasse CustomStreamListener() oder in Haupt Programm nicht initialisiert .

Sie können es in Hauptprogramm initialisieren und es auf die Klasse in einer solchen Art und Weise passieren:

count=<some value> 
class CustomStreamListener(tweepy.StreamListener): 
    def __init__(self,count): 
     self.count=count 

    def on_status(self, status): 
     print ('Got a Tweet') 
     self.count += 1 
     tweet = status.text 
     tweet = self.pattern.sub(' ',tweet) 
     words = tweet.split() 
     for word in words: 
      if len(word) > 2 and word != '' and word not in self.common: 
       if word not in self.all_words: 
        self.all_words[word] = 1 
      else: 
       self.all_words[word] += 1 
Verwandte Themen