ARKAPRAVA GHOSH
ARKAPRAVA GHOSH

Reputation: 1

Filtering sentences from a text file containg a given keyword. Lines are separated by inverted commas

I have accessed data from twitter using tweepy in python and I have stored it in a text file. The text file contains tweets from 30 twitter handles and is inverted comma separated. I now want to select only those posts which contain the keyword "@Tcs" in it and store it in a new text file.

The following is the code for accessing the tweets and storing it in a text file.

import tweepy
import json
import json
import csv

# Consumer keys and access tokens, used for OAuth
CONSUMER_KEY = "86YITDk6xVLNZOizWf"
CONSUMER_SECRET = "exf2t2f7txKObmOlYqrHRFt820D4kflCGzCxOStNyKXf"
ACCESS_TOKEN = "578789959-shMwxAvEZc3JW9c3F9QzbBnjwneIqsRo88"
ACCESS_SECRET = "XGXhI80cX30P20RYjuedicbrcBoFKzfkM5ckW"

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
List_Screen=['@livemint', '@ReutersIndia', '@EconomicTimes', '@NDTVProfit', '    @forbes_india','@moneycontrolcom','@ETNOWlive', ' 
 @ETmarkets','@Investopedia','@BloombergTVInd']
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
with open('data.txt', 'w') as outfile:
   for i in range(len(List_Screen)):
      for status in tweepy.Cursor(api.user_timeline, 
screen_name=List_Screen[i]).items(10):
         print status._json['text']
         data=status._json['text']
         json.dump(data, outfile)

This is screenshot of the text file which needs to be filtered.

Upvotes: 0

Views: 168

Answers (1)

T4rk1n
T4rk1n

Reputation: 1460

You can use a list comprehension.

Considering each tweet is a string in a list:

tweet_list = [tweet for tweet in data if '@Tcs' in tweet]

Upvotes: 1

Related Questions