Python Twitter json文本过滤器

时间:2015-02-16 15:38:45

标签: python twitter

使用Twitter的流媒体API,我可以获得JSON格式的数据。 但是,我很难使用python从数据集中过滤掉某些关键字。以下就是我所做的。

首先我定义了空字符串和列表进行过滤:

tweets=[]
tweetStr=''
tweetsFiltered=[]

然后我做的是打开json文件,将其追加到tweets = []下面:

for line in open('apple.json'):
    try:
        tweets.append(json.loads(line))
    except:
        pass

然后对于推文数据,我希望过滤关键字并删除

filterKeyword=['eat','cinnamon','fruit','pie','juice']
for tweet in tweets:
    for tweet['text'] in tweet:
        for key in filterKeyword:
            if key in tweet['text']:
                pass
            else:
                tweetsFiltered.append(tweet)
                tweetStr+=str(tweet['text'])
print(tweetStr)

但它只返回JSON文件中的键(我认为它是字典键),就像这个

timestamp_mstimestamp_mstimestamp_mstimestamp_mstimestamp_msretweetedretweetedretweetedretweetedretweetedin_reply_to_user_id_strin_reply_to_user_id_strin_reply_to_user_id_strin_reply_to_user_id_strin_reply_to_user_id_strtruncatedtruncatedtruncatedtruncatedtruncatedretweeted_statusretweeted_status

在此代码中,我如何删除某些关键字并保留主要数据"推文" 或添加 tweetStr ...

0 个答案:

没有答案