0

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

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

我试图从文件中提取@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']

4

1 回答 1

1

如果您不想使用re,请采纳 Andrew 的建议

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

否则,请参阅标记的副本。


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

因为你显然不能使用filteror lambda

于 2019-05-01T21:31:24.667 回答