×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

python reading between specific lines

python reading between specific lines

python reading between specific lines

(OP)
I have read a file. Now I have all these lines of strings. I need to extract specific lines. These lines are between two lines with a specific string in them. There is no set amount of lines between them. Also, I have to match the two lines. For example, There may be the string in the first line, but there is no match, so I will get the same string after a number of lines. Then I have to see if that line has the match. I have no clue how to do this.

RE: python reading between specific lines

Quote (berrybub)

I have read a file. Now I have all these lines of strings.
The "right way" to have done that is to have a list of lines of strings. Let's assume that's what you did. So now you're looking for the indices of the elements that contain the string. So let the list of lines be "lstLines", and let the key string be "strKey". Then the indices, in order, of where the string is found is:

CODE -->

lstIx = [lstLines.index(i) for i in lstLines if strKey in i] 

_________________
Bob Rashkin

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close