question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Emoji encoding on streaming API

See original GitHub issue

Hi, I am using a very simple example of the streaming API, but it is failing on the encoding of a tweet with emojis. My code is roughly the following:

class Listener(StreamListener):

    def on_data(self, data):
        self.buffer.append(data)
        print("hello", len(self.buffer))
        print(json.loads(data))

l = Listener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

stream = Stream(auth, l)
stream.filter(follow = [user_id],
              stall_warnings = True,
              encoding = 'unicode-escape')

It produces this error

File "streaming.py", line 29, in on_data print(json.loads(data)) File "C:\Users\Felipe\Anaconda2\envs\py35\lib\encodings\cp850.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in position 2228-2231: character maps to <undefined>

I have tried various encodings including the by default utf-8 but the method still fails. Is there any way to enconde emjois properly?.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Satyam-Bhallacommented, Oct 8, 2017

You are getting this error because the python shell and cmd can’t encode emojis so if you try to print out a emoji it will not print that and you will get this error. I was also suffering from the same situation but I just found the solution. Try to implement this in a Jupyter notebook it will work fine. Just follow these commands… pip3 install --upgrade pip3 install jupyter After installing jupyter go to the cmd and type jupyter notebook It will open the web browser there you can create a new notebook and in that notebook place all the code above and run the kernel. You will not get any error. More about Jupyter

0reactions
Harmon758commented, Apr 27, 2019

This is an issue with your console’s code page/encoding rather than an issue with Tweepy. You’re attempting to print a character outside the range that CP 850 supports. This is easily resolved by changing the active code page to UTF-8 using chcp and specifying 65001.

@nuvious This is an unrelated issue that should now be fixed with https://github.com/tweepy/tweepy/commit/0279a4e911db0bf54268366a0cb6ae65ad5f8988 (#606) as part of Tweepy v3.4.0. See #549.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Real-Time Streaming of Every Emoji Posted as a Comment to ...
Let's say we want to find every emoji as it's posted to Reddit. First, we'll define what an emoji is. The Unicode Consortium...
Read more >
Struggling to handle emoji in tweepy script due to encoding ...
A string is fine. All (I think) that I need to know is how an emoji should be pulled from the csv to...
Read more >
Twitter data encoding and fonts - Standard APIs v1.1
Twitter data encoding and fonts ... Using streaming API to get social data and presenting it to UI after some analytical operations. Having...
Read more >
Sugar support for emojis (a.k.a. the Unicode Supplementary ...
Character encoding and the beginnings of Unicode ... As you type away on your keyboard, the stream of characters that gets stored in...
Read more >
How I Kept Building Emojitracker
Updates to UTF encoding of Emoji (“variant encoding”) that offered a new ... Streams from Twitter Streaming API: handles connections, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found