Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43977
| Date | 2013-04-20 19:34 -0500 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: There must be a better way |
| References | <kkv9bt$9bm$1@theodyn.ncf.ca> <51732d81$0$29977$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.867.1366504376.3114.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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