I have been working with the Tweepy and when I execute the filter of the tweepy passing non-ascii characters I have an error. For exemple, using the following command, I got the error below:
My code:
auth = tweepy.OAuthHandler(apikey[0], apikey[1])
auth.set_access_token(apikey[2], apikey[3])
api = tweepy.API(auth)
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['eleições'])
My error:
Traceback (most recent call last):
File "./TwitterStreamingAPI.py", line 81, in <module>
sapi.filter(track=['eleições'])
File "/usr/local/lib/python2.7/dist-packages/tweepy/streaming.py", line 303, in filter
encoded_track = [s.encode(encoding) for s in track]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 4: ordinal not in range(128)
I would be glad if someone help me in how to solve it.
Thanks in advance,
Thiago.
Try replacing the filter line with:
One does
.encode()
to convert fromunicode
tostr
. One does.decode()
to convert fromstr
tounicode
. Since tweepy is attempting to.encode()
, we should feed it aunicode
.