[Tutor] Redux: optparse
spir
denis.spir at free.fr
Sat Dec 27 21:48:13 CET 2008
Le samedi 27 décembre 2008 à 14:55 -0500, Matt Herzog a écrit :
> On Wed, Dec 24, 2008 at 01:12:55AM -0000, Alan Gauld wrote:
> >
> > "Kent Johnson" <kent37 at tds.net> wrote
> >
> > >> for filename in os.listdir(directory):
> > >> result = re.match(s, filename)
> > >> print result
> > >
> > >You never open and read the files. You are searching for the pattern
> > >in the filename, not in the contents of the file.
> >
> > Also note that match() only searches starting at the start of the
> > string.
> >
> > Thus match will find foo at
> >
> > foobar
> >
> > but not in
> >
> > sofoo
> >
> > You usually need to use search() to find the pattern anywhere
> > within the string.
> >
> > Also look at the thread earlier this week on using listdir() and
> > the fileinput module.
>
> Hello again and thanks for the encouragement.
>
> I have been working on this problem again today and switched to the fileinput method. What I can't figure out now is how to pass a compiled regex to an optparse option. I'm confused ias to "option" versus "arg" when using the optparse module. In fact there seems to be no way to define what the arg should be; only options. Is the arg always implied? I have read several pages on optparse and am none the wiser.
>
> How do I fix the rx = re.compile('-x') line below so that the string I pass on the command line gets passed into the re.compile?
>
> #!/usr/bin/python
> import fileinput, sys, string, optparse, re
>
> #def main():
> optparser = optparse.OptionParser()
> optparser.add_option("-x", "--regx", help="regular expression")
> # take the first argument out of sys.argv and assign it to searchterm
> #searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:]
> (options, args) = optparser.parse_args()
Do you want to use optparse, or get the command line arguments yourself?
It seems the pattern string will be the first arg, will it?
> rx = re.compile('-x')
Then, if the note above is right, you have:
pat_string = sys.argv[1]
rx = re.compile(pat_string) # pattern object
Is that what you wish to do?
> for line in fileinput.input():
> num_matches = string.count(line, rx)
Above you are counting the number of *regex pattern* may be inside a
string. Not the number of (sub)strings matching the pattern, which is
probably what you intend.[ Also, you are using a deprecated function
"count" of the string module. Use line.count(substring) instead. Anyway,
this is probably not what you want.]
To count the number of pattern matches, you must use the
"matching" (lol) regex method, namely findall:
result_list = rx.findall(line)
> if num_matches:
> print "found '%s' %d times in %s on line %d." % (rx, num_matches,
> fileinput.filename(), fileinput.filelineno())
Do you want to print only the last match? If not, you need to record
matches found by the search loop into a list.
> #if __name__ == '__main__':
> #main()
More information about the Tutor
mailing list