Need advice performing a search
Remco Gerlich
scarblac-spamtrap at pino.selwerd.nl
Wed Apr 26 04:48:44 EDT 2000
Klaus Bruns wrote in comp.lang.python:
> mybe try this:import re
>
> file = open("file","r")
>
> alllines = file.readlines()
> pattern1 = re.compile("first_string") ## first_string as regualr
> expression
> pattern2 = re.compile("secound_string") ## secound_string as regular
> expression
>
> result = []
>
> for line in alllines:
> if pattern1.search(line) or pattern2.search(line):
> result.append(line)
>
> print result ## result is maybe ['line1','line2','line3']
I think using regular expressions is overkill for simply searching a
substring. Use string.find(string, substring) instead?
file = open("file","r")
alllines = file.readlines()
file.close()
from string import find
def testline(line):
return find(line, "first_string") or find(line, "second_string")
print filter(testline, alllines)
Is another way to do it.
--
Remco Gerlich, scarblac at pino.selwerd.nl
Hi! I'm a .sig virus! Join the fun and copy me into yours!
More information about the Python-list
mailing list