[Tutor] Searching in a file

Hugo Arts hugo.yoshi at gmail.com
Thu Jan 14 00:29:04 CET 2010

On Thu, Jan 14, 2010 at 12:05 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> I prefer the next() approach.

Rightfully so. IMO, The while loop with readline is basically the C
version of that, for the poor people who don't have iterators.

> But a third option is to use a split and apply it to the whole file as
> a string thereby breaking the file into as many chunks as start with
> a line containing 'NEW'...
> data = open(....).read()
> for chunk in data.split('NEW'):
>    for line in chunk.split('\n'):  # create lines out of the chunks - you
> may not need this
>         # process lines till done
> Just a thought.

Probably the simplest option by far. Disadvantage is you'll have to
load the entire file into memory. Whether that is important depends
entirely on the file. But unless the file is extremely large, it
shouldn't be a big problem.


More information about the Tutor mailing list