Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

python reading between specific lines

Status
Not open for further replies.

berrybub

Programmer
Mar 11, 2013
2
US
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.
 
berrybub said:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top