[Tutor] Possible to search text file for multiple string values at once?
Scott Stueben
sidewalking at gmail.com
Fri Jan 23 20:11:33 CET 2009
Thanks for the help so far - it seems easy enough. To clarify on the
points you have asked me about:
A sqlite3 database on my machine would be an excellent idea for
personal use. I would like to be able to get a functional script for
others on my team to use, so maybe a script or compiled program
(Win32) eventually.
As for output, I would probably like to return the entire lines that
contain any search results of those strings. Maybe just output to a
results.txt that would have the entire line of each line that contains
'Bob', 'John', 'Joe', 'Jim', and or 'Fred'.
Speed isn't as important as ease of use, I suppose, since
non-technical people should be able to use it, ideally.
Maybe, since I am on Win32, I could have a popup window that asks for
input filename and path, and then asks for string(s) to search for,
and then it would process the search and output all lines to a file.
Something like that is what I am imagining, but I am open to
suggestions on items that may be easier to use or code.
Is that reasonably simple to code for a beginner?
Thanks again,
Scott
On Fri, Jan 23, 2009 at 11:52 AM, Kent Johnson <kent37 at tds.net> wrote:
> On Fri, Jan 23, 2009 at 1:25 PM, Scott Stueben <sidewalking at gmail.com> wrote:
>
>> I would like to search a text file for a list of strings, like a sql query.
>
> What do you want to do if you find one? Do you want to get every line
> that contains any of the strings, or a list of which strings are
> found, or just find out if any of the strings are there?
>
>> For instance: To search a text file for the values 'Bob', 'John',
>> 'Joe', 'Jim', and 'Fred', you would have to open the dialog and do
>> five separate searches. Lots of copying and pasting, lots of room for
>> typos.
>
> You can do this with a regular expression. For example,
>
> import re
> findAny = re.compile('Bob|John|Joe|Jim|Fred')
>
> for found in findAny.findall(s):
> print found
>
> will print all occurrences of any of the target names.
>
> You can build the regex string dynamically from user input; if
> 'toFind' is a list of target words, use
> findAny = re.compile('|'.join(re.escape(target) for target in toFind))
>
> re.escape() cleans up targets that have special characters in them.
>
--
"Shine on me baby, cause it's rainin' in my heart"
--Elliott Smith
More information about the Tutor
mailing list