我有一长串字符串,其中包含按给定顺序排列的感兴趣的子字符串,但这里有一个在文本文件中使用句子的小例子:
This is a long drawn out sentence needed to emphasize a topic I am trying to learn.
It is new idea for me and I need your help with it please!
Thank you so much in advance, I really appreciate it.
从这个文本文件,我想找到同时包含任何句子"I"和"need",但他们必须在出现的顺序。
因此,在这个例子中,'I'并且'need'都发生在第1句和第2句,但句子1他们是在错误的顺序,所以我不想返回。我只想'I need'按顺序返回第二句话。
我用这个例子来识别子字符串,但我不知道如何只按顺序找到它们:
id1 = "I"
id2 = "need"
with open('fun.txt') as f:
for line in f:
if id1 and id2 in line:
print(line[:-1])
这将返回:
This is a long drawn out sentence needed to emphasize a topic I am trying to learn.
It is new idea for me and I need your help with it please!
但我只想:
It is new idea for me and I need your help with it please!
谢谢!
慕盖茨4494581
慕无忌1623718
相关分类