Julian Silvestri
Julian Silvestri

Reputation: 2027

Tweepy, UnicodeEncodeError USC-2

So I am attempting to make a twitter bot using python and the tweepy package. However, I keep getting a UnicodeEncodeError. The error occurs because python cannot read/understand the emojis passed into the tweet that I have pulled via a stream. I have searched all the other questions with simillar problems but each solution posted does not work for my given code. The closest I have gotten is using this code here found here('UCS-2' codec can't encode characters in position 1050-1050)

non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)

However I am still getting the error even though I am able to see some em

    import tweepy
    from unicodedata import normalize, unicodedata
    from tweepy import OAuthHandler
    from tweepy import StreamListener
    from tweepy import Stream
    import sys




    #Variables for each required KEY, secret and token
    consumer_key = ""

    consumer_secret = ""

    access_token = ""

    access_token_secret = ""

    #Set up OAuth and integrate with API
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

    auth.set_access_token(access_token, access_token_secret)

    api = tweepy.API(auth)


    class MyStreamListener(tweepy.StreamListener):

        def on_status(self, status):
            print(status.text)

        def on_error(self, status_code):

            if status_code == 420:
                return False

    non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
    myStream = tweepy.Stream(auth=api.auth,listener = MyStreamListener())
    start_stream = myStream.filter(track=['Trump'],async=True)

    print(str(start_stream).translate(non_bmp_map))

    #write a tweet to push to account
    #tweet = "I just felt like running!"
    #api.update_status(status=tweet)

here is the error code I receive:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Python36-32\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 294, in _run
    raise exception
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 263, in _run
    self._read_loop(resp)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 324, in _read_loop
    self._data(next_status_obj)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 297, in _data
    if self.listener.on_data(data) is False:
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\tweepy\streaming.py", line 54, in on_data
    if self.on_status(status) is False:
  File "C:\Users\jsilvest\Documents\Python\compugenTwitterBot.py", line 32, in on_status
    print(status.text)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\run.py", line 345, in write
    return self.shell.write(s, self.tags)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\rpc.py", line 604, in __call__
    value = self.sockio.remotecall(self.oid, self.name, args, kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\rpc.py", line 216, in remotecall
    return self.asyncreturn(seq)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\rpc.py", line 247, in asyncreturn
    return self.decoderesponse(response)
  File "C:\Program Files (x86)\Python36-32\lib\idlelib\rpc.py", line 267, in decoderesponse
    raise what
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 138-138: Non-BMP character not supported in Tk

Has anyone solved this issue ?

I am using windows 10 and running Python3

Upvotes: 2

Views: 348

Answers (1)

Julian Silvestri
Julian Silvestri

Reputation: 2027

Answer IS...Courtesy of @Mark Tolonen

Do not use IDLE when trying to use Tweepy

It is not able to support emojis.

Try using a different IDE such as PyCharm. I was successful using PyCharm and I am sure other IDE's that are not IDLE will also support it.

Thank you Mark Tolonen

Upvotes: 1

Related Questions