GeoBeez
GeoBeez

Reputation: 1014

TypeError: Can't convert 'bytes' object to str implicitly

I want to use the tweepy API for streaming data from tweeter and I use this video (http://sentdex.com/sentiment-analysisbig-data-and-python-tutorials-algorithmic-trading/how-to-use-the-twitter-api-1-1-to-stream-tweets-in-python/ ) to learn how to do that, but unfortunately I got this Error,

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

ckey = 'credentials'
csecret = 'you'
atoken = 'should'
asecret = 'invalidate'

class listener(StreamListener):

    def on_data(self, data):
        print (data)
        return True

    def on_error(self, status):
        print (status)

auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["car"])

and this is the Error:

Traceback (most recent call last):
  File "C:/Users/azamb/PycharmProjects/PyStream/Stream.py", line 24, in <module>
    twitterStream.filter(track=["car"])
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 418, in filter
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 335, in _start
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 275, in _run
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 244, in _run
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 287, in _read_loop
  File "C:\Python34\lib\site-packages\tweepy-2.3-py3.4.egg\tweepy\streaming.py", line 167, in read_line
TypeError: Can't convert 'bytes' object to str implicitly

Upvotes: 0

Views: 1258

Answers (2)

Maghoumi
Maghoumi

Reputation: 3380

Just thought I'd share my experience with this. A friend of mine was dealing with a similar problem with tweepy. The problem was with the stream.py file (where Python was throwing errors). It's probably a problem with an old tweepy version but in that file, read_line and read_len functions were defective. .decode('utf-8') had to be added manually as shown below:

def read_len(self, length):
        while not self._stream.closed:
            if len(self._buffer) >= length:
                return self._pop(length)
            read_len = max(self._chunk_size, length - len(self._buffer))
            # #####
            # HERE 
            # #####
            self._buffer += self._stream.read(read_len).decode('utf-8')

def read_line(self, sep='\n'):
    start = 0
    while not self._stream.closed:
        loc = self._buffer.find(sep, start)
        if loc >= 0:
            return self._pop(loc + len(sep))
        else:
            start = len(self._buffer)
        # #########
        # AND HERE 
        # #########
        self._buffer += self._stream.read(self._chunk_size).decode('utf-8')

Upvotes: 1

GeoBeez
GeoBeez

Reputation: 1014

I updated the API and installed it again! somebody improved that. it works now :)

Upvotes: 1

Related Questions