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.
My test.txt file
Now using open to read the file and than applying
regular expressions
.Instead of using regular expression use Python
string.find
method.str.find
and its siblingrfind
havestart
andend
args.This is the fastest way if the contained text is short and near the front.
If the contained text is relatively large, use:
If the contained text is short and near the end, use:
The first method is in any case better than the naive method of starting the second search from the start of the text; use it if your contained text has no dominant pattern.