Emoji encoding on streaming API
See original GitHub issueHi, 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:
- Created 6 years ago
- Reactions:5
- Comments:5 (1 by maintainers)
Top GitHub Comments
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 typejupyter 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 JupyterThis 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 specifying65001
.@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.