Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81685
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'subject:text': 0.05; 'context': 0.07; 'discard': 0.07; 'subject:file': 0.07; "'#'": 0.09; 'caller': 0.09; 'line:': 0.09; 'python': 0.11; 'def': 0.12; 'at,': 0.16; 'empty,': 0.16; 'generator.': 0.16; 'line.split()': 0.16; 'paradigm.': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'basically': 0.19; '(in': 0.22; 'header:User-Agent:1': 0.23; 'space.': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; "i'm": 0.30; 'getting': 0.31; 'lines': 0.31; 'subject:some': 0.31; 'becomes': 0.33; "i'd": 0.34; 'etc': 0.35; 'but': 0.35; 'yield': 0.36; 'should': 0.36; 'wrong': 0.37; 'starting': 0.37; 'manager': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'is.': 0.60; 'solve': 0.60; 'simply': 0.61; "you're": 0.61; 'feeling': 0.68; 'received:74.208': 0.68; 'received:74.208.4.194': 0.84; 'subject:over': 0.84; 'subject:via': 0.84 |
| Date | Fri, 28 Nov 2014 10:22:18 -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 | <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; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:RxRJEiYdlhldr3jY1Qbm2m80siysqvYAvHIMQwiEO3V 3GfK5lQmL+lXLITRF3Wb7HlXeAk6dd9fKIHi6hynwztAauOHq8 6fk8lmGVOsSSIJlq7WNhhLX1q/9Rj72QfyUkaF1dGeimHp9Gqk NEoqasaizlgAiPFMmhEJXMBTe0+1Jb0aTZKXpvcW2SCEffVcdL ULYTb0YF2fbU91A+BmC9RvvtsWyXREs7iDr0MEW6PX3PKH4fMi GcwgUWgIKvC1aKlZfjWkQyBCsz7+vVbyFMSCMCNBfDUbH9H6/E k6rMEwUTq0NUlSDHCf8S8J8TzwmFg1XzivOr8CVJkmYwhebb2X cK2BFYoKrWqEFitO/DVM= |
| 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.16406.1417188158.18130.python-list@python.org> (permalink) |
| Lines | 50 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417188158 news.xs4all.nl 2850 [2001:888:2000:d::a6]:41087 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81685 |
Show key headers only | View raw
On 11/28/2014 10: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:
>
> for line in open( 'myfile' ):
> 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
>
> process( items )
>
> You see I'd like to ignore lines which are empty, start with a #, or
> are only white space. How would I write a context manager so that the
> above simply becomes
>
> with some_tricky_stuff( 'myfile' ) as items:
> process( items )
>
I see what you're getting at, but a context manager is the wrong
paradigm. What you want is a generator. (untested)
def mygenerator(filename):
with open(filename) as f:
for line in f:
if not line: continue
if line.startswith('#'): continue
items = line.split()
if not items: continue
yield items
Now your caller simply does:
for items in mygenerator(filename):
process(items)
--
DaveA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Iterate over text file, discarding some lines via context manager Dave Angel <davea@davea.name> - 2014-11-28 10:22 -0500
csiph-web