getting the line just before or after a pattern searched

Alex Martelli aleaxit at yahoo.com
Thu Feb 16 23:10:48 EST 2006


<s99999999s2003 at yahoo.com> wrote:

> hi
> 
> i have a file something like this
> 
> abcdefgh
> ijklmnopq
> 12345678
> rstuvwxyz
> .....
> .....
> .....
> 12345678
> .....
> 
> whenever i search the file and reach 12345678, how do i get the line
> just above and below ( or more than 1 line above/below) the pattern
> 12345678 and save to variables? thanks

If the file's of reasonable size, read it all into memory into a list of
lines with a .readlines method call, then loop with an index over said
list and when you find your pattern at index i use i-1, i+1, and so on
(just check that the resulting i+N or i-N is >=0 and <len(thelist)).

If the file's too big to keep in memory at once, you need more clever
approaches -- keep a collections.deque of the last M lines read to be
able to get "N lines ago" for N<M, and for getting lines "after" the one
of interest (which you will read only in "future" iterations) keep track
of relative linenumbers that "will" interest you, decrement them with
each line read, trigger when they reach 0.  But unless you're dealing
with files of many, MANY hundres of megabytes, on any typical modern
machine you should be OK with the first, WAY simpler approach.


Alex



More information about the Python-list mailing list