Record seperator

Terry Reedy tjreedy at udel.edu
Sat Aug 27 22:03:44 CEST 2011


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 = []

-- 
Terry Jan Reedy




More information about the Python-list mailing list