re.search - just skip it

Kent Johnson kent3737 at yahoo.com
Wed Jan 26 17:21:40 CET 2005


rasdj at frontiernet.net wrote:
> Input is this:
> 
> SET1_S_W CHAR(1) NOT NULL,
> SET2_S_W CHAR(1) NOT NULL,
> SET3_S_W CHAR(1) NOT NULL,
> SET4_S_W CHAR(1) NOT NULL,
> ;
> 
> .py says:
> 
> import re, string, sys
> s_ora = re.compile('.*S_W.*')
> lines = open("y.sql").readlines()
> for i in range(len(lines)):
> try:
> if s_ora.search(lines[i]): del lines[i]

When you delete for example lines[0], the indices of the following lines change. So the former 
lines[1] is now lines[0] and will not be checked.

The simplest way to do this is with a list comprehension:
lines = [ line for line in lines if not s_ora.search(line) ]

Even better, there is no need to make the intermediate list of all lines, you can say
lines = [ line for line in open("y.sql") if not s_ora.search(line) ]

In Python 2.4 you don't have to make a list at all, you can just say
open("z.sql","w").writelines(line for line in open("y.sql") if not s_ora.search(line))

;)

Kent

> except IndexError:
> open("z.sql","w").writelines(lines)
> 
> but output is:
> 
> SET2_S_W CHAR(1) NOT NULL,
> SET4_S_W CHAR(1) NOT NULL,
> ;
> 
> It should delete every, not every other!
> 
> thx,
> 
> RasDJ
> 



More information about the Python-list mailing list