Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81701
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <torriem+gmail@torriefamily.org> |
| 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; 'discard': 0.07; 'see:': 0.07; 'subject:file': 0.07; "'#'": 0.09; 'line:': 0.09; 'python': 0.11; 'def': 0.12; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'input:': 0.16; 'url:dabeaz': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'basically': 0.19; '(in': 0.22; 'header:User-Agent:1': 0.23; 'sort': 0.25; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "i'm": 0.30; 'lines': 0.31; 'subject:some': 0.31; "i'd": 0.34; 'etc': 0.35; 'but': 0.35; 'yield': 0.36; 'should': 0.36; 'starting': 0.37; 'message-id:@gmail.com': 0.38; 'manager': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'is.': 0.60; 'solve': 0.60; 'feeling': 0.68; 'subject:over': 0.84; 'subject:via': 0.84; 'thing,': 0.91 |
| X-Virus-Scanned | amavisd-new at torriefamily.org |
| Date | Fri, 28 Nov 2014 11:24:07 -0700 |
| From | Michael Torrie <torriem@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131118 Thunderbird/17.0.11 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Iterate over text file, discarding some lines via context manager |
| References | <CADjSo4Ro3xFj9T9gj0NO7YESE6G873vvrBD=Y=OAk-jNBRD5Bw@mail.gmail.com> |
| In-Reply-To | <CADjSo4Ro3xFj9T9gj0NO7YESE6G873vvrBD=Y=OAk-jNBRD5Bw@mail.gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 7bit |
| 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.16416.1417199058.18130.python-list@python.org> (permalink) |
| Lines | 29 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417199058 news.xs4all.nl 2955 [2001:888:2000:d::a6]:43362 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81701 |
Show key headers only | View raw
On 11/28/2014 08:04 AM, fetchinson . wrote:
> Hi all,
>
> I have a feeling that I should solve this by a context manager but
> since I've never used them I'm not sure what the optimal (in the
> python sense) solution is. So basically what I do all the time is
> this:
I'd personally do it with a generator function.
def filter(input):
for line in input:
if not line:
# discard empty lines
continue
if line.startswith( '#' ):
# discard lines starting with #
continue
items = line.split( )
if not items:
# discard lines with only spaces, tabs, etc
continue
yield items
for line_items in filter(open( 'myfile' )):
process( items )
For an excellent presentation on this sort of thing, see:
http://www.dabeaz.com/generators/
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Iterate over text file, discarding some lines via context manager Michael Torrie <torriem@gmail.com> - 2014-11-28 11:24 -0700
csiph-web