Record seperator

Roy Smith roy at panix.com
Sat Aug 27 23:07:48 CEST 2011


In article <mailman.477.1314475482.27778.python-list at python.org>,
 Terry Reedy <tjreedy at udel.edu> wrote:

> On 8/27/2011 1:45 PM, Roy Smith wrote:
> > In article<4e592852$0$29965$c3e8da3$5496439d at news.astraweb.com>,
> >   Steven D'Aprano<steve+comp.lang.python at pearwood.info>  wrote:
> >
> >> open("file.txt")   # opens the file
> >>   .read()           # reads the contents of the file
> >>   .split("\n\n")    # splits the text on double-newlines.
> >
> > The biggest problem with this code is that read() slurps the entire file
> > into a string.  That's fine for moderately sized files, but will fail
> > (or at least be grossly inefficient) for very large files.
> 
> I read the above as separating the file into paragraphs, as indicated by 
> blank lines.
> 
> def paragraphs(file):
>    para = []
>    for line in file:
>      if line:
>        para.append(line)
>      else:
>        yield para # or ''.join(para), as desired
>        para = []

Plus or minus the last paragraph in the file :-)



More information about the Python-list mailing list