* 'struct-like' list *

Schüle Daniel uval at rz.uni-karlsruhe.de
Tue Feb 7 11:57:54 EST 2006


Ernesto wrote:
> Thanks for the approach.  I decided to use regular expressions.  I'm
> going by the code you posted (below).  I replaced the line re.findall
> line with my file handle read( ) like this:
> 
> print re.findall(pattern, myFileHandle.read())
> 
> This prints out only brackets [].  Is a 're.compile' perhaps necessary
> ?

if you see [] that means findall didn't find anything
that would match your pattern
if you re.compile your pattern beforehand that
would not make findall find the matched text
it's only there for the optimization

consider
lines = [line for line in file("foo.txt").readlines() if 
re.match(r"\d+",line)]

in this case it's better to pre-compile regexp one and use it
to match all lines

number = re.compile(r"\d+")
lines = [line for line in file("foo.txt").readlines() if number.match(line)]

fire interactive python and play with re and patterns
speaking from own experience ... the propability is
against you that you will make pattern right on first time

Regards, Daniel




More information about the Python-list mailing list