使用文本文件查找特定短语后的单词,直到不同的短语

时间:2015-03-19 14:22:40

标签: python

有没有办法找到某个短语,然后使用以下信息(仅在它之后)作为输出,但只能使用不同的指定短语?抱歉,如果它令人困惑!

1 个答案:

答案 0 :(得分:1)

例如:

string = "Is there a way to find a certain phrase, " \
         "then use the following information (only after it) " \
         "as an output, but only up until a different " \
         "specified phrase? sorry if its confusing!"
first_phrase = "certain phrase"
second_phrase = "a different specified phrase"

intermediate = (string[string.find(first_phrase)+len(first_phrase):])
final = intermediate[0:intermediate.find(second_phrase)]

print final

最终结果是:

, then use the following information (only after it) as an output, but only up until 

这是你想要的。你可以重构代码来摆脱中间变量,但我想让它可读。