Python amateur here. I have a text file that lists information on thousands of lines and I’m trying to select a line and the following 2-3 lines based on whether they match a pattern.
I’ve filtered the file down from the original to just contain the parts of the file of interest to me so my current file looks like this:
trig1.RESP:
stim4: silence.wav
trig1.RESP:
trig6.RESP: 1
trig1.RESP:
trig1.RESP:
trig5.RESP: 1
stim5: silence.wav
trig1.RESP:
trig6.RESP: 1
stim3: silence.wav
trig1.RESP:
stim5: silence.wav
trig1.RESP:
trig6.RESP: 1
and so on and so forth…
Basically what I’m trying to do is write every line that contains the silence.wav portion of the line and then the next two lines after it. I used the following code:
parsed_output = open("name-of-file-to-be-written", "w")
filtered_input = open("name-of-file-that-has-above-data", "r")
for line in filtered_input:
if "silence.wav" in line and "trig1" in filtered_input.next():
parsed_output.write(line)
parsed_output.write(filtered_input.next())
parsed_output.close()
This works fine for the most part because it prints the silence.wav line and the line which has the response (the part I’m most interested in, the trig1 before a response at this point is less important). However the issue I run into is when the lines go:
stim3: silence.wav
trig1.RESP:
stim5: silence.wav
Since my output will then write the stim3 (current line) and stim5 (next line after skipping the trig1), I think it moves on to the next “stim:silence.wav” and skips the stim5 because it was included in the previous command when it was written.
I want the trig6.RESP: 1 after the stim5 but my output doesn’t show it for that reason I described.
Is there a way I can get it to not skip over that stim5?
Sorry if this was really long. Thank you in advance!
How about something like this? (completely untested)
It’s not fancy, but I think it should be pretty robust.