I have about 150 text files filled with character information. Each file contains two unique words ()alpha and bravo and i want to extract the text between these unique words and write it to a different file.
Manually i can CTRL+F for the two words and copy the text between, i just want to know how to do this using a program (preferably Python) for many files.
You can use regular expressions for that.
>>> st = "alpha here is my text bravo" >>> import re >>> re.findall(r'alpha(.*?)bravo',st) [' here is my text ']
My test.txt file
alpha here is my line yipee bravo
Now using open to read the file and than applying
>>> f = open('test.txt','r') >>> data = f.read() >>> x = re.findall(r'alpha(.*?)bravo',data,re.DOTALL) >>> x [' here is my line\nyipee\n'] >>> "".join(x).replace('\n',' ') ' here is my line yipee ' >>>
a = 'alpha' b = 'bravo' text = 'from alpha all the way to bravo and beyond.' text.split(a)[-1].split(b) # ' all the way to '