Record seperator

Terry Reedy tjreedy at udel.edu
Sun Aug 28 02:55:55 CEST 2011


On 8/27/2011 5:07 PM, Roy Smith wrote:
> 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 :-)

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

-- 
Terry Jan Reedy




More information about the Python-list mailing list