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

spir denis.spir at free.fr
Sat Jan 24 10:16:51 CET 2009


Le Fri, 23 Jan 2009 14:45:32 -0600,
W W <srilyk at gmail.com> a écrit :

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

I guess the easiest for your team would be to:
* let the script write the result lines into a text file
* let the script open the result in an editor (using module called subprocess)
* put a link to your script on the desk

### just an example
# write to file
target  = open("target.txt",'w')
for line in lines:
	target.write(line)
target.close()

# open in editor
import subprocess
subprocess.call(["gedit","target.txt"])
print "*** end ***"

denis

------
la vida e estranya


More information about the Tutor mailing list