Lets say I have a Text file with the below content
fdsjhgjhg fdshkjhk Start Good Morning Hello World End dashjkhjk dsfjkhk Start hgjkkl dfghjjk
You can do this with regular expressions. This will exclude rogue Start and End lines. Here is a live example
Start
End
import re f = open('test.txt','r') txt = f.read() matches = re.findall(r'^\s*Start\s*$\n((?:^\s*(?!Start).*$\n)*?)^\s*End\s*$', txt, flags=re.M)