[Tutor] search/match file position q

Alan Gauld alan.gauld at btinternet.com
Tue Oct 7 11:47:00 CEST 2014


On 07/10/14 00:55, Clayton Kirkwood wrote:

> I haven't been able to find an definitive answer.

There isn't one.
Each technique has its place. It depends on what you
are doing and why. And to answer that you need to ask a
definitive question.

> I am looking through a file(stream:<), for several matching strings.

Using what? regex? Or string search methods? Or character by character?
or a parser?

> Match definitively starts at the beginning of the stream.
Sounds like regex except regex doesn't work on streams it works
on strings. You could read the string(s) from a stream but that's
not what regex does.

> As I search, or match, do I start over at the beginning of the stream
 > for each match or do I start at the end of the last
> search/match?

Whichever fits your design best.

Usually using regex I just use findall() and let Python do the work.

> Apparently the file object knows where it is in a stream, but
> it is unclear whether line_location = readline() keeps track of that
> location.

All read operations move the cursor forward and the file keeps
track of it.  But that's all irrelevant for match() and search()
they don't care about the file.

> I see advantages to both protocols, but overall, I would prefer
> that the next search starts at the end of the last start

If that's what you want then that's what you should do.
Now what is preventing you? Do you have code where you
try to do it? If so we might see the problem.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list