How to read from a file to an arbitrary delimiter efficiently?

Steven D'Aprano steve at pearwood.info
Sat Feb 27 05:40:17 EST 2016


On Thu, 25 Feb 2016 06:37 pm, Wolfgang Maier wrote:

> On 25.02.2016 07:50, Steven D'Aprano wrote:
>> I have a need to read to an arbitrary delimiter, which might be any of a
>> (small) set of characters. For the sake of the exercise, lets say it is
>> either ! or ? (for example).
>>
> 
> You are not alone with your need.
> 
> http://bugs.python.org/issue1152248 discusses the problem and has some
> code snippets that you may be interested in. While there is no trivial
> solution there are certainly faster ways than your first attempt.

Wow. Ten years and still no solution :-(

Thanks for finding the issue, but the solutions given don't suit my use
case. I don't want an iterator that operates on pre-read blocks, I want
something that will read a record from a file, and leave the file pointer
one entry past the end of the record.

Oh, and records are likely fairly short, but there may be a lot of them.


-- 
Steven



More information about the Python-list mailing list