searching for multiple strings in one pass
Russell E. Owen
owen at astroNOJNK.washington.edu.invalid
Fri Jul 21 12:51:00 EDT 2000
In article <8l7db8$v3g$1 at nnrp1.deja.com>, ghost_man at my-deja.com wrote:
>I am stuck on a problem here where I am using python to grab a large
>amount of info from a database. I am then using python to pass command
>line parameters to some program based on file names. I only want to
>run on certain file types such as files ending with .hpp or .cpp or
>whatever. The problem is how can I do searches for multiple file types
>in one pass rather than having to run through the entire list several
>times? Here is a sample of how I am doing it now....
>
>hpp = string.find(newline,".hpp")
> if hpp > 0:
> k = (creating argumenthere)
> os.system (k)
>
>How do I check for several file types in one pass?
Not knowing the details of the data you're parsing, two ways come to
mind. There are undoubtedly others.
1) Use regular expressions, i.e. the "re" module. In the example you
gave, you could look for:
\.(?P<ext>(hpp)|(cpp)|(etc))
to match the extension. (?P<ext>...) labels the found text; if you call
the groupdict() method on the match object, you'll get a dictionary, one
of whose keys is "ext" -- the found extension.
If the file name is buried in the line (rather than being all of it) and
you need it, you can extract the entire file name in the same regular
expression. And I'm pretty sure those labelled subexpressions nest, so
you can get the entire file name and the extension at the same time.
2) If the file names appear in an obvious place in string (such as each
string is a file name all by itself), then another approach to consider
is string.split to split up the file names into names and extensions and
then just an if statement on the extension. For example:
partlist = split(newline, ".")
ext = partlist[-1]
if ext in ["hpp", "cpp", ...]:
Regards,
-- Russell
More information about the Python-list
mailing list