Extreme newbie.. looking for a push in the right direction. Need to find text in a file and then display

Alex Martelli aleax at aleax.it
Tue Aug 28 04:55:10 EDT 2001


"toflat" <toflatpy2 at oaktown.org> wrote in message
news:48dbc3f6.0108272358.19747602 at posting.google.com...
> Hey There,
>
> I still pretty new to this and hope this question isn't too lame. I've

Absolutely not!  Please don't worry -- beginners are welcome here.

My compliments for the very accurate problem description.  Few
problems are specified so well -- really!  I'm not repeating it
here, interested people can refer to your post.

> understand how to use
> the re module to find what I'm looking for. However I'm at a loss when

Don't use regular expressions when you can avoid them, they're
complicated -- use just strings if they can do what you need
(and, here, they can).

> it come to
> actually using what it finds. I don't know how I can get it to save
> the line that contains
> the search result. And I do not know how to get it to keep adding
> lines (and modifying) until it comes to the end marker.

You can build a list of the results, although this is not
needed if all you want is display the results on standard
output (you can just write them out as you find them).

Here is an example that returns a list, written as a function
that takes the tagname to look for and the already-opened
file in which to look.  I'm using Python 2.1, there may be
slight differences needed if you use older Python versions.

def lookfor(infile, atagname):
    tomatch = "<%s>\n" % atagname
    endmatch = "</end %s>\n" $ atagname
    result = []
    file_lines = infine.readlines()
    line_number = 0
    for line_number in range(len(file_lines)):
        if file_lines[line_number] == tomatch:
            break
    else:    # if no break, then not found
        return result    # so, empty result list
    # now, line_number is the number of the
    # line with the opening "<name>\n".  As
    # per your specs, the results start with
    # the following line then a blank one:
    result.append(file_lines[line_number+1])
    result.append('')
    # and now, loop until the end tag matches:
    for line in file_lines[line_number+2:]:
        if line == endmatch:
            return result    # we're done
        result.append(line)
    else:   # no end-tag was found, what then?
        return result    # means 'all to end'?

As you see there are a few doubts left (even
in the very best specs, as usual:-), namely
what to do on error/anomalies -- what if the
open tag is never found, or if it is but the
close tag isn't.  I've solved them as came
most naturally here (empty result list for
no-open-tag found, everything to the end if
the close-tag is not found), but you should
of course review this for your specific case.

An example of use of this function might be:

thefile = open('myfile.txt')
lines = lookfor(thefile, 'name')
if not lines:
    print "<name> not found in myfile.txt"
else:
    for line in lines:
        print line


Alex






More information about the Python-list mailing list