Trying to pull a twitter handle from a text file

40 views Asked by At

I am trying to extract a set of alpha numeric characters from a text file.

below would be some lines in the file. I want to extract the '@' as well as anything that follows.

im trying to pull @bob from a file. this is a @line in the @file @bob is a wierdo

the below code is what I have so far.

def getAllPeople(fileName):
    #give empty list
    allPeople=[]
    #open TweetsFile.txt
    with open(fileName, 'r') as f1:
        lines=f1.readlines()
        #split all words into strings
        for word in lines:
            char = word.split("@")
            print(char)
    #close the file
    f1.close()

What I am trying to get is; ['@bob','@line','@file', '@bob']

1

There are 1 answers

3
modesitt On

If you do not want to use re, take Andrew's suggestion

mentions = list(filter(lambda x: x.startswith('@'), tweet.split()))

otherwise, see the marked duplicate.


mentions = [w for w in tweet.split() if w.startswith('@')]

since you apparently can not use filter or lambda.