[Tutor] Re: print out lines that start with a word

Liam Clarke cyresse at gmail.com
Wed Feb 9 09:05:00 CET 2005


regexes are common across a lot of languages, even Java has them.
Though the pain that would be I daren't not imagine. So the syntax is
familiar, whereas string methods may not be.


On Wed, 09 Feb 2005 09:00:52 +0100, Wolfram Kraus
<kraus at hagen-partner.de> wrote:
> Ron Nixon wrote:
> > Can anyone tell me what I've done wrong in this
> > script.
> >
> > 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
> >
> >
> Pardon my ignorance, but why is everybody fond of regexps ;-) ? Are they
> faster? What about good ol' startswith():
> http://docs.python.org/lib/string-methods.html#l2h-204
> Untested:
> 
> f = open('c:/lines.txt').readlines()
> for line in f:
>    if line.startswith('This'):
>      print line # Or whatever match is, no regexp-expert here, sorry
> 
> Wondering,
> Wolfram
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list