On 8/27/2011 5:07 PM, Roy Smith wrote:
In article<mailman.477.1314475482.27778.python-l...@python.org>,
  Terry Reedy<tjre...@udel.edu>  wrote:

On 8/27/2011 1:45 PM, Roy Smith wrote:
In article<4e592852$0$29965$c3e8da3$54964...@news.astraweb.com>,
   Steven D'Aprano<steve+comp.lang.pyt...@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 :-)

Or right, I forgot the last line, which is a repeat of the yield after the for loop finishes.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to