Parsing Text file

Denis McMahon denismfmcmahon at gmail.com
Tue Jul 2 20:55:22 EDT 2013


On Tue, 02 Jul 2013 13:28:33 -0700, sas429s wrote:

> Ok here is a snippet of the text file I have:
> I hope this helps..
> .....
> Thanks for your help

ok ... so you need to figure out how best to distinguish the filename, 
then loop through the file, remember each filename as you find it, and 
when you find lines containing your target text, print the current value 
of filename and the target text line.

filenames might be distinguished by one or more of the following:

They always start in column 0 and nothing else starts in column 0
They never contain spaces and all other lines contain spaces or are blank
They always contain at least one / characters
They always terminate with a . followed by one or more characters
All the characters in them are lower case

Then loop through the file in something like the following manner:

open input file;
open output file;
for each line in input file: {
	if line is a filename: {
		thisfile = line; }
	elif line matches search term: {
		print thisfile in output file;
		print line in output file; } }
close input file;
close output file;

(Note this is an algorithm written in a sort of pythonic manner, rather 
than actual python code - also because some newsreaders may break 
indenting etc, I've used ; as line terminators and {} to group blocks)

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list