Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tweepy-在列表中搜索字符串-可能存在编码问题_Python_Twitter_Ascii_Tweepy - Fatal编程技术网

Python Tweepy-在列表中搜索字符串-可能存在编码问题

Python Tweepy-在列表中搜索字符串-可能存在编码问题,python,twitter,ascii,tweepy,Python,Twitter,Ascii,Tweepy,我试图过滤掉从结果列表返回的重复(转发)推文。我想我可以简单地创建一个列表,检查tweet的文本是否已经在列表中,如果没有,那么我就将其添加到列表中。我尝试这样做的代码部分如下- searched_tweets = tweepy.Cursor(api.search, q=search, since=since_time, include_entities=True).items(max_tweets) filtered_tweets =[] for filtered in searched_

我试图过滤掉从结果列表返回的重复(转发)推文。我想我可以简单地创建一个列表,检查tweet的文本是否已经在列表中,如果没有,那么我就将其添加到列表中。我尝试这样做的代码部分如下-

searched_tweets = tweepy.Cursor(api.search, q=search, since=since_time, include_entities=True).items(max_tweets)

filtered_tweets =[]

for filtered in searched_tweets:

    if str(filtered_tweets).find(str(filtered.text.encode('ascii', 'ignore')))== -1:
        filtered_tweets.append(filtered)

这不起作用,因为它本质上总是返回true,即使帖子已经存在。我怀疑这是由于文本编码?因为我搜索的结束编码字符串基本上已经删除了部分?欢迎就如何克服这一问题提供任何建议。

过滤转发的更简单方法是:

for tweet in searched_tweets:    
    if hasattr(tweet,"retweeted_status"):
        continue

    #if you have reached this line, your tweet is not a retweet
    #do stuff with your tweet

过滤转发的更简单方法是:

for tweet in searched_tweets:    
    if hasattr(tweet,"retweeted_status"):
        continue

    #if you have reached this line, your tweet is not a retweet
    #do stuff with your tweet

是的,也许编码是个问题。为什么不先在
unicode
中同时编码引用和子字符串(在
find
之前)?谢谢ahmad,我不确定我能不能(或知道怎么做),因为我不能对列表进行编码,你的意思是每次循环列表并将项目编码为ascii吗?将这三行放在第一行,靠近导入的地方,要将编码全局更改为UTF-8:1)
import sys
,2)
reload(sys)
,3)
sys.setdefaultencoding('utf8')
。是的,也许编码就是问题所在。为什么不先在
unicode
中同时编码引用和子字符串(在
find
之前)?谢谢ahmad,我不确定我能不能(或知道怎么做),因为我不能对列表进行编码,你的意思是每次循环列表并将项目编码为ascii吗?将这三行放在第一行,靠近导入的地方,要将编码全局更改为UTF-8,请执行以下操作:1)
import sys
,2)
reload(sys)
,3)
sys.setdefaultencoding('utf8')