[Tutor] print out lines that start with a word

Alan Gauld alan.gauld at freenet.co.uk
Wed Feb 9 23:07:17 CET 2005

> I'm trying to get only the lines that start with
> "This" for a text file.
> Here's what I wrote:
> >>> import re
> >>> f = open('c:/lines.txt').readlines()
> >>> for line in f:
> match = re.search('^This',f)
> if line == match:
> print match


     if line.startwith('This'):

its easier!
But your problem above is that match returns a match object 
which is not a string. So you can't compare directly you 
need to extract the string from inside the match object!


Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list