[Tutor] Re: print out lines that start with a word
Wolfram Kraus
kraus at hagen-partner.de
Wed Feb 9 09:00:52 CET 2005
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
More information about the Tutor
mailing list