[Tutor] Possible to search text file for multiple string valuesat once?
Alan Gauld
alan.gauld at btinternet.com
Tue Jan 27 23:38:00 CET 2009
"Scott Stueben" <sidewalking at gmail.com> wrote
> 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).
Thats pretty straightforward to do.
In pseudo code:
fileB = open(raw_input("Filename to search: "))
searches = [s for s in open("FileA.txt")]
results = Set()
for line in fileB:
for s in searches:
if s in line: results.add(line+'\n')
open("FileC.txt").writelines(results)
Not the most efficient solution but that can be tweaked
using some of the other suggestions from earlier.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list