Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with selecting multiple lines of a txt document and write to new text doc in python

Tags:

python

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!

like image 457
user1867442 Avatar asked Nov 30 '12 20:11

user1867442


People also ask

How do you write multiple lines in a text file in Python?

Using writelines() Function This function writes several string lines to a text file simultaneously. An iterable object, such as a list, set, tuple, etc., can be sent to the writelines() method.

How do you create a new line in a text file in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.


2 Answers

How about something like this? (completely untested)

count = 3
for line in filtered_input:
    if "silence.wav" in line:
        count = 0
    else:
        count += 1

    if count <= 2:
        filtered_output.write(line)

It's not fancy, but I think it should be pretty robust.

like image 160
mgilson Avatar answered Sep 21 '22 06:09

mgilson


My attempt at translating this to psuedocode says:

For each (Line) {
      If Next Line is "Trig1" AND Current Line is "silence.wav"
          Log it
}
## And We're Done

(Feel free to correct me here)

You're missing the Trig6 because you're asking for the next line that doesn't exist. Could you rewrite it where you refer backwards instead of forwards and have that fix your problem?

like image 40
Raye Keslensky Avatar answered Sep 21 '22 06:09

Raye Keslensky