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: 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 To: Steven D'Aprano Subject: Re: There must be a better way In-Reply-To: <51732d81$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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