尝试从文本文件中提取Twitter句柄

时间:2019-05-01 21:25:31

标签: python-3.x wing-ide

我正在尝试从文本文件中提取一组字母数字字符。

下面将是文件中的某些行。我想提取“ @”以及随后的所有内容。

im试图从文件中提取@bob。 这是@file中的@line @bob是个怪人

下面的代码是我到目前为止所拥有的。

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()

我想要得到的是; ['@bob','@ line','@ file','@bob']

1 个答案:

答案 0 :(得分:1)

如果您不想使用re,请采纳安德鲁的建议

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

否则,请查看标记为重复的副本。


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

因为您显然不能使用filterlambda

相关问题