There must be a better way

Colin J. Williams cjw at ncf.ca
Sun Apr 21 11:30:53 EDT 2013


On 21/04/2013 9:43 AM, Peter Otten wrote:
> Colin J. Williams wrote:
>
>> I was seeking some code that would be acceptable to both Python 2.7 and
>> 3.3.
>>
>> In the end, I used:
>>
>> inData= csv.reader(inFile)
>>
>> def main():
>>       if ver == '2':
>>           headerLine= inData.next()
>>       else:
>>           headerLine= inData.__next__()
>>       ...
>
> I think it was mentioned before, but to be explicit:
>
> def main():
>      headerLine = next(inData)
>      ...
>
> works in Python 2.6, 2.7, and 3.x.
>

Yes, the penny dropped eventually.  I've used your statement

The Chris suggestion was slightly different:

Use the built-in next() function
(http://docs.python.org/2/library/functions.html#next ) instead:
     headerLine = next(iter(inData))

Colin W.



More information about the Python-list mailing list