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@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to