Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12323

Re: Record seperator

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Record seperator
Date 2011-08-27 20:55 -0400
References (2 earlier) <slrnj5i1g9.581.greymausg@hmaus.org> <4e592852$0$29965$c3e8da3$5496439d@news.astraweb.com> <roy-F7BDDC.13453127082011@news.panix.com> <mailman.477.1314475482.27778.python-list@python.org> <roy-098B9B.17074827082011@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.496.1314493010.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 8/27/2011 5:07 PM, Roy Smith wrote:
> In article<mailman.477.1314475482.27778.python-list@python.org>,
>   Terry Reedy<tjreedy@udel.edu>  wrote:
>
>> On 8/27/2011 1:45 PM, Roy Smith wrote:
>>> In article<4e592852$0$29965$c3e8da3$5496439d@news.astraweb.com>,
>>>    Steven D'Aprano<steve+comp.lang.python@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Record seperator greymaus <greymausg@mail.com> - 2011-08-26 18:39 +0000
  Re: Record seperator "D'Arcy J.M. Cain" <darcy@druid.net> - 2011-08-26 15:02 -0400
    Re: Record seperator greymaus <greymausg@mail.com> - 2011-08-27 16:59 +0000
      Re: Record seperator Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-28 03:24 +1000
        Re: Record seperator Roy Smith <roy@panix.com> - 2011-08-27 13:45 -0400
          Re: Record seperator ChasBrown <cbrown@cbrownsystems.com> - 2011-08-27 11:40 -0700
          Re: Record seperator Terry Reedy <tjreedy@udel.edu> - 2011-08-27 16:03 -0400
            Re: Record seperator Roy Smith <roy@panix.com> - 2011-08-27 17:07 -0400
              Re: Record seperator Terry Reedy <tjreedy@udel.edu> - 2011-08-27 20:55 -0400
          Re: Record seperator Chris Angelico <rosuav@gmail.com> - 2011-08-28 06:07 +1000
        Re: Record seperator greymaus <greymausg@mail.com> - 2011-08-28 10:03 +0000

csiph-web