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


Groups > comp.lang.python > #81697

Re: Iterate over text file, discarding some lines via context manager

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:text': 0.05; 'context': 0.07; 'responding': 0.07; 'subject:file': 0.07; 'function,': 0.09; 'reached.': 0.09; 'python': 0.11; 'def': 0.12; 'thread': 0.14; 'ast': 0.16; 'iterator': 0.16; 'self.line': 0.16; 'thread?': 0.16; 'true:': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'earlier': 0.24; 'file.': 0.24; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'raise': 0.29; 'lines': 0.31; 'subject:some': 0.31; 'file': 0.32; 'class': 0.32; 'skip:_ 10': 0.34; 'maybe': 0.34; 'message.': 0.35; 'operate': 0.35; 'but': 0.35; 'there': 0.35; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'new': 0.61; "you're": 0.61; 'you.': 0.62; 'here': 0.66; 'received:74.208': 0.68; 'to,': 0.72; 'subject:over': 0.84; 'subject:via': 0.84
Date Fri, 28 Nov 2014 12:40:38 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Iterate over text file, discarding some lines via context manager
References <mailman.16405.1417187079.18130.python-list@python.org> <5478ab81$0$2879$426a34cc@news.free.fr>
In-Reply-To <5478ab81$0$2879$426a34cc@news.free.fr>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:VMSEv1+zKj6axZxRuywzHRE1Gk88IAAG+/EJFIBdzTK 55kyKrxm4Kayiqn9U0SMpKjbGossWeoNnGrEehtds8edgdHem2 DFl//K4vkhhcqIfdu/k3TXBag0UZgMrxQRFafZhpLQnKAusXaP Aug6eXlfGBJASiCnjpEHQUz0YVw8BKCms52XujiJPm9Bhe2f75 wbC45NSJhAKk5sI/Kinbd2WaJQxzikITbtXbRDTA2TTf/GyQPW xcJcnrfObTUvnlUghRdJQfXX1Kw1rtr3eOStw6veSKLGaTjxEI xohLcXd3c9VHRV/0Xi3+rvm4Ss6/oxCzPcLmVmom85rP0r1DPT kAUkSo/0e7u4UuuT1loc=
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.16414.1417196454.18130.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417196454 news.xs4all.nl 2937 [2001:888:2000:d::a6]:59062
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 4342
X-Received-Body-CRC 4204876086
Xref csiph.com comp.lang.python:81697

Show key headers only | View raw


Please don't start a new thread when responding to an existing topic. 
Just reply-List to the message you're responding to, and include some 
context from that message.

On 11/28/2014 12:06 PM, ast wrote:
> Hi
>
> Here is a solution with a custom iterator which operate on files.
> I tested it with a small file. Just a remark, there are no empty line
> on a file, there is at least '\n' at the end of each lines but maybe the
> last one. If readline() got an emptyline, then the end of file has been
> reached.
>
>
> class MyFileItr:
>     def __init__(self, f):
>         self.f = f
>         self.line = ""
>
>     def __next__(self):
>
>         while True:
>             self.line = self.f.readline()
>
>             if (self.line == ''):
>                 raise StopIteration
>             if not(self.line.startswith('#') or self.line.isspace() or
> self.line == '\n'):
>                 return self.line
>
>     def __iter__(self):
>         return self
>
>
> f = open('test.txt', 'r')
>
> for L in MyFileItr(f):
>     print(L, end="")
>

Why would you prefer that over a generator function, as given earlier in 
the thread?  See for example Ned's message.  By using 'yield', you get 
Python to generate all the class boilerplate for you.



-- 
DaveA

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


Thread

Iterate over text file, discarding some lines via context manager "fetchinson ." <fetchinson@googlemail.com> - 2014-11-28 16:04 +0100
  Re: Iterate over text file, discarding some lines via context manager "ast" <nomail@invalid.com> - 2014-11-28 18:06 +0100
    Re: Iterate over text file, discarding some lines via context manager Dave Angel <davea@davea.name> - 2014-11-28 12:40 -0500
      Re: Iterate over text file, discarding some lines via context manager Marko Rauhamaa <marko@pacujo.net> - 2014-11-28 19:55 +0200
        Re: Iterate over text file, discarding some lines via context manager Chris Angelico <rosuav@gmail.com> - 2014-11-29 05:02 +1100
          Re: Iterate over text file, discarding some lines via context manager Marko Rauhamaa <marko@pacujo.net> - 2014-11-28 20:04 +0200

csiph-web