[Tutor] Redux: optparse
Matt Herzog
msh at blisses.org
Sat Dec 27 22:46:59 CET 2008
> 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?
Again I am confused. I assumed that optparse was the best way to pass in arguments (such as filenames) from the command line. Like so:
./script.py -x regex *.html
> > 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?
Probablement. :) The below code does what I want:
-------------------------------------------------------------------------------
#!/usr/bin/python
import fileinput, sys, string
# Take the first argument out of sys.argv and assign it to searchterm.
searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:]
for line in fileinput.input():
num_matches = line.count(searchterm)
if num_matches: # A nonzero count means there was a match.
print "found '%s' %d times in %s on line %d." % (searchterm, num_matches, fileinput.filename(), fileinput.filelineno())
-------------------------------------------------------------------------------
I wanted to use optparse instead of sys.argv. Perhaps I should study fileinput instead. I don't understand that either.
> > 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()
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
I fear you speak upon the rack,
Where men enforced do speak anything.
- William Shakespeare
More information about the Tutor
mailing list