[Tutor] Finding a specific line in a body of text

Emile van Sebille emile at fenx.com
Mon Mar 12 14:22:43 CET 2012


On 3/11/2012 6:56 PM Robert Sjoblom said...
> I'm sorry if the subject is vague, but I can't really explain it very
> well. I've been away from programming for a while now (I got a
> daughter and a year after that a son, so I've been busy with family
> matters). As such, my skills are definitely rusty.
>
> In the file I'm parsing, I'm looking for specific lines. I don't know
> the content of these lines but I do know the content that appears two
> lines before. As such I thought that maybe I'd flag for a found line
> and then flag the next two lines as well, like so:
>


If, as others suggest, the files do fit in memory, you might try:

content = open(thisfile).read()
for fragment in content.split(keyword)[1:]:
     myresults.append(fragment.split('\n')[1]


Emile




More information about the Tutor mailing list