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


Groups > comp.lang.python > #43977

Re: There must be a better way

Path csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'finally:': 0.07; 'versions,': 0.07; 'absent': 0.09; 'f.close()': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '2.7': 0.14; 'changes': 0.15; '-tkc': 0.16; 'class)': 0.16; 'csv': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'module?': 0.16; 'nameerror:': 0.16; 'subject:There': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; '2.x': 0.24; 'basis,': 0.24; 'headers': 0.24; 'own.': 0.24; 'williams': 0.24; 'header': 0.24; 'regardless': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.25; 'define': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'expansion': 0.30; 'code': 0.31; '3.x': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'regular': 0.32; 'run': 0.32; 'cases': 0.33; 'older': 0.33; '"the': 0.34; 'common': 0.35; 'except': 0.35; 'offered': 0.35; 'no,': 0.35; '2.6': 0.36; 'consistent': 0.36; 'next': 0.36; 'method': 0.36; 'shows': 0.36; 'charset:us-ascii': 0.36; 'does': 0.39; "couldn't": 0.39; 'either': 0.39; 'skip:n 10': 0.64; 'our': 0.64; 'talking': 0.65; 'normal.': 0.68; 'lack': 0.78; 'received:50.22': 0.84; 'usage.': 0.84; '2013': 0.98
Date Sat, 20 Apr 2013 19:34:22 -0500
From Tim Chase <python.list@tim.thechases.com>
To Steven D'Aprano <steve+comp.lang.python@pearwood.info>
Subject Re: There must be a better way
In-Reply-To <51732d81$0$29977$c3e8da3$5496439d@news.astraweb.com>
References <kkv9bt$9bm$1@theodyn.ncf.ca> <51732d81$0$29977$c3e8da3$5496439d@news.astraweb.com>
X-Mailer Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
Cc python-list@python.org
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.867.1366504376.3114.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366504376 news.xs4all.nl 2256 [2001:888:2000:d::a6]:60555
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43977

Show key headers only | View raw


On 2013-04-21 00:06, Steven D'Aprano wrote:
> On Sat, 20 Apr 2013 19:46:07 -0400, Colin J. Williams wrote:
> 
> > Below is part of a script which shows the changes made to permit
> > the script to run on either Python 2.7 or Python 3.2.
> > 
> > I was surprised to see that the CSV next method is no longer
> > available.
> 
> This makes no sense. What's "the CSV next method"? Are you talking
> about the csv module? It has no "next method".

In 2.x, the csv.reader() class (and csv.DictReader() class) offered
a .next() method that is absent in 3.x  For those who use(d) the
csv.reader object on a regular basis, this was a pretty common
usage.  Particularly if you had to do your own header parsing:

  f = open(...)
  r = csv.reader(f)
  try:
    headers = r.next()
    header_map = analyze(headers)
    for row in r:
      foo = row[header_map["FOO COLUMN"]]
      process(foo)
  finally:
    f.close()

(I did this for a number of cases where the client couldn't
consistently send column-headers in a consistent
capitalization/spaces, so my header-making function had to normalize
the case/spaces and then reference the normalized names)

> So provided you are using Python 2.6 or better, you call:
> 
> next(inData)
> 
> to get the next value, regardless of whether it is Python 2.x or
> 3.x.
> 
> If you need to support older versions, you can do this:
> 
> try:
>     next  # Does the built-in already exist?
> except NameError:
>     # No, we define our own.
>     def next(iterator):
>         return iterator.next()
> 
> then just use next(inData) as normal.

This is a good expansion of Chris Rebert's suggestion to use next(),
as those of us that have to support pre-2.6 code lack the next()
function out of the box.

-tkc



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


Thread

There must be a better way "Colin J. Williams" <cjw@ncf.ca> - 2013-04-20 19:46 -0400
  Re: There must be a better way Chris Rebert <clp2@rebertia.com> - 2013-04-20 16:57 -0700
  Re: There must be a better way Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-21 00:06 +0000
    Re: There must be a better way Tim Chase <python.list@tim.thechases.com> - 2013-04-20 19:34 -0500
    Re: There must be a better way Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-20 21:07 -0400
      Re: There must be a better way "Colin J. Williams" <cjw@ncf.ca> - 2013-04-21 09:15 -0400
        Re: There must be a better way Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-04-21 16:39 +0300
          Re: There must be a better way "Colin J. Williams" <cjw@ncf.ca> - 2013-04-21 11:17 -0400
        Re: There must be a better way Peter Otten <__peter__@web.de> - 2013-04-21 15:43 +0200
          Re: There must be a better way "Colin J. Williams" <cjw@ncf.ca> - 2013-04-21 11:30 -0400
          Re: There must be a better way "Colin J. Williams" <cjw@ncf.ca> - 2013-04-21 11:30 -0400
        Re: There must be a better way Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-22 15:32 +0100
        Re: There must be a better way Neil Cerutti <neilc@norwich.edu> - 2013-04-22 14:42 +0000
          Re: There must be a better way "Colin J. Williams" <cjw@ncf.ca> - 2013-04-22 13:44 -0400
            Re: There must be a better way Neil Cerutti <neilc@norwich.edu> - 2013-04-23 13:36 +0000
              Re: There must be a better way Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 15:15 +0100
              Re: There must be a better way Tim Chase <python.list@tim.thechases.com> - 2013-04-23 09:30 -0500
              Re: There must be a better way Skip Montanaro <skip@pobox.com> - 2013-04-23 09:36 -0500
              Re: There must be a better way (correction) Tim Chase <python.list@tim.thechases.com> - 2013-04-23 10:02 -0500

csiph-web