On 8/27/2011 5:07 PM, Roy Smith wrote:
In article<[email protected]>,
  Terry Reedy<[email protected]>  wrote:

On 8/27/2011 1:45 PM, Roy Smith wrote:
In article<[email protected]>,
   Steven D'Aprano<[email protected]>   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