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


Groups > comp.lang.python > #103662

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

Path csiph.com!news.mixmin.net!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!fu-berlin.de!uni-berlin.de!not-for-mail
From Tim Delaney <timothy.c.delaney@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to read from a file to an arbitrary delimiter efficiently?
Date Mon, 29 Feb 2016 08:00:22 +1100
Lines 36
Message-ID <mailman.26.1456693224.9760.python-list@python.org> (permalink)
References <56cea44e$0$11128$c3e8da3@news.astraweb.com> <CAHVvXxQ-iTv=OAWDtnzQ0BKG2mcSK7-bDKXiaVP2r1kSCm8+nA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de axMz3fmDxKqmpgGYSULiuQHhUpWuXitv96DU7EL6NxtA==
Return-Path <timothy.c.delaney@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'method.': 0.05; 'chunk': 0.07; 'subject:file': 0.07; 'subject:How': 0.09; 'benjamin': 0.09; 'buffer,': 0.09; 'delimiter': 0.09; 'sake': 0.09; 'underlying': 0.09; '(small)': 0.16; '100x': 0.16; '2016': 0.16; "chris'": 0.16; 'delimiters': 0.16; 'dropping': 0.16; 'efficiently.': 0.16; 'example).': 0.16; 'expressions.': 0.16; 'iterating': 0.16; 'mmap': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'stream.': 0.16; 'to:name:python list': 0.16; 'wrote:': 0.16; '&gt;': 0.18; 'email addr:gmail.com&gt;': 0.18; 'input': 0.18; 'otherwise,': 0.20; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'regular': 0.29; "i'm": 0.30; 'probably': 0.31; "d'aprano": 0.33; 'lets': 0.33; 'steven': 0.33; 'though.': 0.33; '(for': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'attempt': 0.35; 'whilst': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'there': 0.36; 'lines': 0.36; 'faster': 0.36; 'to:addr:python- list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'files': 0.38; 'end': 0.39; 'sure': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'back': 0.62; 'leaving': 0.63; 'times': 0.63; '100': 0.79; 'delaney': 0.84; 'exercise,': 0.84; 'oscar': 0.84; 'overhead,': 0.84; 'subject:read': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=GzUDlvqONtVylVhb3xRwbsgHLWXM+MdyWwFPUF6K9q4=; b=UFIN4KdZugz3t/mRWJvruXTgOU0ueRIiYDdXeYOWz0TALr45I8BbzG2X6IRi5wcEc3 jeak0Ra5FDCeYKg6/TxoEaDOR6bWWyIRDMteD71YdmCs6xj6k1TnHaTnZ1oclwWYjb5p jzPRWrCrw2rPuLcJCn147gRoZNMGR6py7HYWLCBA1UbBLU3ABwHroExiBf5DL2pihkhp lyv1dr8gesSZeg/5nY3CN4e+HDDa4g3952ZRDzdxKlWAbP+29GUs/LzOmTTEXQxflaE8 r0EhEs38ebeKE50EUQ3ovoRZRbWjOIclS4MnEbgDBteLzAqjBLIE/SluyOB2tMfEfm7I 9ZyA==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=GzUDlvqONtVylVhb3xRwbsgHLWXM+MdyWwFPUF6K9q4=; b=iH/7pZBNSf4YAf2O6O8S0CYIGoSD/PRYeaf7jQPOKlPVUV5xAIHkwziTmmLamw5sHI H85BWa1R/Z+h0V71I8i+DuDvSq52L0hOCD43at6+y1SHWF6EpTt1DerUk7V5/6v/9xtR X01TLxzwwQWSpdhdpY4rEBbCA8SCH8Pk8CjfUJC1vWRtyqUIh84UO/XBXiM2eubMzQgS l+6uxp6b/bV0VZSS3irndzHygSVy3iVELszn+NlKtX+MENDgM62kRc7SAOVXLqoQ6XTK 9f7kWfTNc59mTz1WJQJYOITQ8siQGg8sW3M/4GIAU90nCnC/mbBcuyuuo1Og7Eh9K4IT BPWg==
X-Gm-Message-State AD7BkJJs+EQAuN2JOjJKAyAwmsRe9+fPn3QcIfTNoUEvINUG5dC9S2MWCSttmic7v6K0pd+lQcwuGT0OaUrUTg==
X-Received by 10.194.59.233 with SMTP id c9mr10887058wjr.88.1456693222200; Sun, 28 Feb 2016 13:00:22 -0800 (PST)
In-Reply-To <CAHVvXxQ-iTv=OAWDtnzQ0BKG2mcSK7-bDKXiaVP2r1kSCm8+nA@mail.gmail.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.21rc2
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103662

Show key headers only | View raw


On 29 February 2016 at 07:28, Oscar Benjamin <oscar.j.benjamin@gmail.com>
wrote:

> On 25 February 2016 at 06:50, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> 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).
> >
> > I want to read from files reasonably efficiently. I don't mind if there
> is a
> > little overhead, but my first attempt is 100 times slower than the
> built-in
> > "read to the end of the line" method.
>
> You can get something much faster using mmap and searching for a
> single delimiter:
>
> My timing makes that ~7x slower than iterating over the lines of the
> file but still around 100x faster than reading individual characters.
> I'm not sure how to generalise it to looking for multiple delimiters
> without dropping back to reading individual characters though.
>

You can use an mmapped file as the input for regular expressions. May or
may not be particularly efficient.

Otherwise, if reading from a file I think read a chunk, and seek() back to
the delimiter is probably going to be most efficient whilst leaving the
file position just after the delimiter.

If reading from a stream, I think Chris' read a chunk and maintain an
internal buffer, and don't give access to the underlying stream.

Tim Delaney

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


Thread

How to read from a file to an arbitrary delimiter efficiently? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-02-25 17:50 +1100
  Re: How to read from a file to an arbitrary delimiter efficiently? Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-02-25 08:37 +0100
    Re: How to read from a file to an arbitrary delimiter efficiently? Steven D'Aprano <steve@pearwood.info> - 2016-02-27 21:40 +1100
      Re: How to read from a file to an arbitrary delimiter efficiently? Dan Sommers <dan@tombstonezero.net> - 2016-02-27 14:40 +0000
      Re: How to read from a file to an arbitrary delimiter efficiently? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-02-27 12:03 -0500
        Re: How to read from a file to an arbitrary delimiter efficiently? Marko Rauhamaa <marko@pacujo.net> - 2016-02-27 19:47 +0200
  Re: How to read from a file to an arbitrary delimiter efficiently? Chris Angelico <rosuav@gmail.com> - 2016-02-25 18:30 +1100
    Re: How to read from a file to an arbitrary delimiter efficiently? Steven D'Aprano <steve@pearwood.info> - 2016-02-27 20:49 +1100
      Re: How to read from a file to an arbitrary delimiter efficiently? Chris Angelico <rosuav@gmail.com> - 2016-02-27 23:17 +1100
      Re: How to read from a file to an arbitrary delimiter efficiently? Chris Angelico <rosuav@gmail.com> - 2016-02-27 23:18 +1100
      Re: How to read from a file to an arbitrary delimiter efficiently? Serhiy Storchaka <storchaka@gmail.com> - 2016-02-27 17:23 +0200
  Re: How to read from a file to an arbitrary delimiter efficiently? Paul Rubin <no.email@nospam.invalid> - 2016-02-24 23:48 -0800
    Re: How to read from a file to an arbitrary delimiter efficiently? wxjmfauth@gmail.com - 2016-02-25 06:37 -0800
    Re: How to read from a file to an arbitrary delimiter efficiently? wxjmfauth@gmail.com - 2016-02-25 06:38 -0800
  Re: How to read from a file to an arbitrary delimiter efficiently? BartC <bc@freeuk.com> - 2016-02-27 16:35 +0000
    Re: How to read from a file to an arbitrary delimiter efficiently? BartC <bc@freeuk.com> - 2016-02-27 20:03 +0000
      Re: How to read from a file to an arbitrary delimiter efficiently? BartC <bc@freeuk.com> - 2016-02-27 20:28 +0000
  Re: How to read from a file to an arbitrary delimiter efficiently? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-02-28 20:28 +0000
  Re: How to read from a file to an arbitrary delimiter efficiently? Tim Delaney <timothy.c.delaney@gmail.com> - 2016-02-29 08:00 +1100

csiph-web