Concerning Regular Expressions

Alex Martelli aleax at mail.comcast.net
Mon Jan 30 00:00:54 EST 2006


Tempo <bradfordh at gmail.com> wrote:

> You are right that my move towards regular expressions was way
> premature, but also this post may too turn out to be a little
> premature. I guessed and checked myself a way to accomplish what I
> needed and I will include it in this post. But first Alex (doesn't have
> to be Alex) 

Uh, I think I DO have to be Alex, sorry.  It's sort of, my job
description, if you will...

> could you tell me if your snipplet and mine would be near
> perfect subsitutes for one another? I believe they accomplish the same
> task of looking for 'R0 -' in the list 'lines', however, as you have
> guessed, I do not know my way around Python very well yet. Here is my
> snipplet:
> 
> log = open('C:\log_0.txt')
> lines = log.readlines()
> import re
> for line in lines:
>     R = re.search(r'^R0', line)
>     if R != None:
>         n = 1
>         print n
> import time
> time.sleep(3)
> log.close()

Your re.search looks for R0 at the beginning of a line, and there is no
hint whatsoever of 'R0 -'.  I.e., your loop here is more like:

for line in lines:
  if line.startswith('R0'):
    print 1

etc.  I won't even guess what the stuff AFTER the loop means;-).


Alex



More information about the Python-list mailing list