thesemicolon icon indicating copy to clipboard operation
thesemicolon copied to clipboard

sequence item 0: expected str instance, bytes found

Open rahulbordoloi opened this issue 4 years ago • 0 comments

Here's my code for python3, the above mentioned error is persisting by all the means I'm trying. Can you review it up once?

#-- coding: utf-8 --

import time
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
from textblob import TextBlob
import matplotlib.pyplot as plt
import re

import test

ckey=test.ckey
csecret=test.csecret
atoken=test.atoken
asecret=test.asecret

def calctime(a):
    return time.time()-a

class listener(StreamListener):
    
    def on_data(self,data):
        global initime
        t=int(calctime(initime))
        all_data=json.loads(data)
        tweet=all_data["text"].encode("utf-8")
        # tweet=all_data["text"].encode("utf-8")     
        tweet=all_data["text"].strip() 
        #username=all_data["user"]["screen_name"]
        tweet=" ".join(re.findall("[a-zA-Z]+", tweet))
        blob=TextBlob(tweet.strip())

        global positive
        global negative     
        global compound  
        global count
        
        count=count+1
        senti=0
        for sen in blob.sentences:
            senti=senti+sen.sentiment.polarity
            if sen.sentiment.polarity >= 0:
                positive=positive+sen.sentiment.polarity   
            else:
                negative=negative+sen.sentiment.polarity  
        compound=compound+senti        
        print(count)
        print(tweet.strip())
        print(senti)
        print(t)
        print(str(positive) + ' ' + str(negative) + ' ' + str(compound)) 
        
    
        plt.axis([ 0, 70, -20,20])
        plt.xlabel('Time')
        plt.ylabel('Sentiment')
        plt.plot([t],[positive],'go',[t] ,[negative],'ro',[t],[compound],'bo')
        plt.show()
        plt.pause(0.0001)
        if count==200:
            return False
        else:
            return True
        
    def on_error(self,status):
        print(status)

"""str="Donal Trump"
str=str.decode('utf-8')
twitterStream.filter(track=[str])

If this still doesn't work, 
try this 
twitterStream.filter(track=[b"Donald Trump"])

or try adding this on the first line of your file """
# -- coding: utf-8 --

positive=0
negative=0
compound=0

count=0
initime=time.time()
plt.ion()

auth=OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)

twitterStream=  Stream(auth, listener(count))
#str="Donald Trump"
#str.encode().decode()
#str=str.decode('utf-8')
#twitterStream.filter(track=[str])
twitterStream.filter(track=[b'Donald Trump'])

rahulbordoloi avatar May 29 '20 06:05 rahulbordoloi