Tweepy过滤器utf-8编码 [英] Tweepy filter utf-8 encoding

查看:119
本文介绍了Tweepy过滤器utf-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Tweepy,当我执行传递非ASCII字符的tweepy的过滤器时,出现错误.例如,使用以下命令,我得到以下错误:

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:

我的代码:

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'])

我的错误:

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.

预先感谢

Thiago.

推荐答案

尝试将过滤器行替换为:

Try replacing the filter line with:

sapi.filter(track=[u'eleições'])

执行.encode()unicode转换为str. .decode()可以将str转换为unicode.由于tweepy尝试.encode(),因此我们应该为它提供unicode.

One does .encode() to convert from unicode to str. One does .decode() to convert from str to unicode. Since tweepy is attempting to .encode(), we should feed it a unicode.

这篇关于Tweepy过滤器utf-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆