[Tutor] Possible to search text file for multiple string values at once?

W W srilyk at gmail.com
Fri Jan 23 21:45:32 CET 2009


On Fri, Jan 23, 2009 at 1:11 PM, Scott Stueben <sidewalking at gmail.com>wrote:

> 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 long as everyone on your team has python installed (or as long as python
is installed on the machines they'll be using), a functional script would be
fairly easy to get rolling. Sqlite is (AFAIK) included with the newer
versions of python by default. Heck, it's on the version I have installed on
my phone! (Cingular 8525). Simply zipping up the directory should provide an
easy enough distribution method. Although, you *could* even write a python
script that does the "install" for them.


> 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'.


The simplest method:

In [5]: f = open('interculturalinterview2.txt', 'r')

In [6]: searchstrings = ('holy', 'hand', 'grenade', 'potato')

In [7]: for line in f.readlines():
   ...:     for word in searchstrings:
   ...:         if word in line:
   ...:             print line
   ...:
   ...:
Hana: have a bonfire n candy apples n make potatoes on a car lol!

Wayne: potatoes on a car?

Hana .: yer lol its fun and they taste nicer lol, you wrap a potato in
tinfoil a
nd put in on the engine of a car and close the bonnet and have the engine
run an
d it cooks it in about 30 mins

 Speed isn't as important as ease of use, I suppose, since
> non-technical people should be able to use it, ideally.


Although that wouldn't be quite so easy to use ;) Of course simple
modifications would provide a little more user friendliness.


> 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.


Using Tkinter (again, which AFAIK comes with all versions of python) is
pretty simple.

import Tkinter, tkFileDialog, tkMessageBox
root = Tkinter.Tk()
root.withdraw()
if tkMessageBox.askyesno("Choose a file?", "Would you like to choose a file
to search?"):
    f = tkFileDialog.askopenfile()

You could also use this method to select a file that contains search
strings, or allow the user to input them in some other way.


> Is that reasonably simple to code for a beginner?
>

Yes, it's fairly simple. If you've had minimal programming experience you
might encounter some bigger problems, but if you've programmed in PHP or
some other language you should find it fairly easy to pick up python, and
use all the commands you'll need.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090123/50a50c8b/attachment.htm>


More information about the Tutor mailing list