So I was looking at ways to "grab" a certain part of a text file with Python, when you only know what comes before and after this particular text. I want something like this answer but for single lines. For example if I have a text file called test.txt
that looks like:
This
is
my
test
file
Then I can use
with open('test.txt') as input_data:
for line in input_data:
if line.strip() == 'is':
break
for line in input_data:
if line.strip() == 'test':
break
print(line)
...and that works fine for grabbing my
, but if my text file is a single line e.g.:
This is my test file
Then it doesn't work. I don't want to grab my
by the string index because I want something that will work only based on knowing what comes before and after that part of the line. I tried looking at a lot of questions but haven't found anything.
Thank you!