There must be a better way

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Apr 22 10:32:03 EDT 2013


On 21 April 2013 14:15, Colin J. Williams <cjw at ncf.ca> wrote:
> In the end, I used:
>
> inData= csv.reader(inFile)
>
> def main():
>
>     if ver == '2':
>         headerLine= inData.next()
>     else:
>         headerLine= inData.__next__()
>     ...
>     for item in inData:
>         assert len(dataStore) == len(item)
>         j= findCardinal(item[10])
>         ...

This may not be relevant for what you're doing but if you use
csv.DictReader there's no need to retrieve the top line separately:

$ cat tmp.csv
a,b,c
1,2,3
4,5,6
$ python
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> with open('tmp.csv', 'rb') as csvfile:
...   for row in csv.DictReader(csvfile):
...     print(row)
...
{'a': '1', 'c': '3', 'b': '2'}
{'a': '4', 'c': '6', 'b': '5'}


Oscar



More information about the Python-list mailing list