Python 查找txt文件中每个句子的最后一个单词

Python 查找txt文件中每个句子的最后一个单词,python,Python,所以我有一个文本文件,里面有推特 我只需要在每行中打印8个或更多字符的最后一个单词,并且单词中没有#:或@ 目前,我可以在文本文件中找到满足这些要求的所有单词,除了只打印句子中的最后一个单词。因此,如果一行包含多个符合要求的单词,我将打印所有单词 这就是我目前的状态 for line in open("tweets.txt"): line_strip = line.strip() for word in line_strip.split(): if len(

所以我有一个文本文件,里面有推特

我只需要在每行中打印8个或更多字符的最后一个单词,并且单词中没有#:或@

目前,我可以在文本文件中找到满足这些要求的所有单词,除了只打印句子中的最后一个单词。因此,如果一行包含多个符合要求的单词,我将打印所有单词

这就是我目前的状态

for line in open("tweets.txt"):
  line_strip = line.strip()
  for word in line_strip.split(): 
    if len(word) >=8 and "#" not in word and ":" not in word and "@" not in word:
      print(word)
输出为:

Candidates
remained
candidates
finished
Watching
couldn't
hangover
disappointing.
但应该是:

remained
finished
couldn't
hangover
disappointing.
感谢您的帮助。

line\u strip.split()
将为您提供一个列表,您可以使用
您的\u列表[-1]
访问此列表中的最后一个元素,然后对每行中的这个词应用您的规则。

对于打开的行(“tweets.txt”):
line_strip=line.strip()
words=[如果len(word)>=8且“#”不在word中且“:”不在word中且“@”不在word中,则在第_strip.split()行中逐字逐字排列]
如果len(字)>0:
打印(字[-1])

向后搜索(在输入序列上使用
反向搜索
),并在找到什么后立即停止(通过
中断
循环)。非常感谢!非常感谢你!