Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:53:17+00:00 2026-06-15T03:53:17+00:00

Python amateur here. I have a text file that lists information on thousands of

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:53:19+00:00Added an answer on June 15, 2026 at 3:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python beginner here. I have a text file that is sorted into columns: fields
Python beginner here, I have a list of lists and want to refer to
Python newbie here. I'm trying to package a console app following this doc .
Python 3 really complicated the whole file reading process, when you have a binary
I'm a born again amateur programming novice trying to learn Python 3 (3.2) using
I have imported a plain-text version of PDF using a Python script, but it
[Python/MySQLdb] - CentOS - Linux - VPS I have a page that parses a
Python regular expression I have a string that contains keywords but sometimes the keywords
Python beginner >>> import oauth2 Traceback (most recent call last): File , line 1,
python newbie here. I'm writing the code to control an experiment that has multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.