Query regarding grep!!
johnzenger at gmail.com
johnzenger at gmail.com
Tue Sep 26 13:52:57 EDT 2006
You could also use itertools:
import itertools
import re
find = re.compile("pattern")
for line in itertools.ifilter(lambda x: find.search(x),
file(filepath)):
print line
Fredrik Lundh wrote:
> bhavya sg wrote:
>
> > I saw in PEP4 of python 2.5 that grep module has gone
> > obsolete in perl 2.5. But I am not able to find an
> > alternative for that.
>
> the grep module has been deprecated for ages (it's been in the lib-old
> non-standard library since at least Python 2.1). The old grep module
> depends on the regex module, which has been deprecated since Python 1.6,
> and which was finally removed in Python 2.5.
>
> > My doubt is "are the other forms of grep like egrep and ggrep be used
> > instead"?
>
> there are no such modules in Python, afaik.
>
> (and that's a question, not a doubt, right?)
>
> if you have Python code that uses the grep module, you can replace it
> with something like:
>
> import re
>
> def simple_grep(filename, pattern):
> find = re.compile(pattern).search
> for index, line in open(filename):
> if find(line):
> print filename, index, line[:-1]
>
> where pattern is an RE-style pattern, not a REGEX-style pattern (see the
> RE documentation for details).
>
> </F>
More information about the Python-list
mailing list