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

Scott Stueben sidewalking at gmail.com
Tue Jan 27 20:34:38 CET 2009


As I think more about how to best do this, I wonder if/how python
script would import File A with search values, File B to be searched,
and write each full line containing any of those results to File C.
The code could be set to look for the same input file (A, with the
search values), and the same filename to write to (File C), thus
leaving only the prompt for the file to be searched (File B).

The user would be responsible for listing the search term variables
into File A prior to running the script.

Is this simple enough?

Scott

On Sat, Jan 24, 2009 at 8:43 PM, Scott Stueben <sidewalking at gmail.com> wrote:
> Excellent ideas...thanks to you all for the input.  I will see what I
> can work out in the next few days and report back.
>
> :)  Scott
>
> On Sat, Jan 24, 2009 at 2:16 AM, spir <denis.spir at free.fr> wrote:
>> 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
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
>
> "Shine on me baby, cause it's rainin' in my heart"
>
>                                                  --Elliott Smith
>



-- 

"Shine on me baby, cause it's rainin' in my heart"

                                                  --Elliott Smith


More information about the Tutor mailing list