1

我正在使用 tweepy 收集随机推文,并且我想过滤掉非字母数字推文。

但为了进行检查,我首先需要将推文转换为字符串。例如,

from tweepy import StreamListener
....

class sListener(StreamListener):
       def on_status(self,status):
            ....
            text = str(status.text)
            if not isAlphanumeric(text):
                ......

但是,使用 str() 将推文转换为字符串本身会导致错误,如果推文是非 ascii 并带有以下消息:

UnicodeEncodeError: 'ascii' codec can't encode character

所以我陷入了一个循环,我需要转换为字符串来过滤非ascii,但由于非ascii,我无法转换为字符串......

我什至不知道推文是什么数据类型...

有人可以帮我吗?

4

3 回答 3

0

看来您的推文编码不是ascii

尝试

text = unicode(status.text)

代替

text = str(status.text)
于 2013-01-17T15:42:59.067 回答
0

我过去也遇到过类似的问题。看看这是否有效:

tweetText = status.text.encode("utf-8")
tweetText = unicode(tweetText, errors='ignore')
于 2016-10-18T20:13:09.227 回答
0

尝试

text = status.text.encode('utf8')
于 2016-10-18T13:05:08.653 回答