【问题标题】:converting tweets to string in Python在 Python 中将推文转换为字符串
【发布时间】:2013-01-01 04:53:04
【问题描述】:

我正在使用 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,我无法转换为字符串......

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

谁能帮帮我?

【问题讨论】:

标签: python twitter unicode tweets tweepy


【解决方案1】:

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

tweetText = status.text.encode("utf-8")
tweetText = unicode(tweetText, errors='ignore')

【讨论】:

    【解决方案2】:

    试试

    text = status.text.encode('utf8')
    

    【讨论】:

      【解决方案3】:

      看来你的推文编码不是ascii

      试试

      text = unicode(status.text)
      

      而不是

      text = str(status.text)
      

      【讨论】:

        猜你喜欢
        • 2017-08-09
        • 2012-03-27
        • 1970-01-01
        • 1970-01-01
        • 2014-01-13
        • 2011-06-10
        • 2021-05-14
        • 2012-06-14
        • 1970-01-01
        相关资源
        最近更新 更多