[Tutor] newb help reading lines from csv

Joel Goldstick joel.goldstick at gmail.com
Mon Oct 22 21:45:02 CEST 2012

On Mon, Oct 22, 2012 at 3:35 PM, Dewhirst, Rob <robdewhirst at gmail.com> wrote:
> Thanks Matt and Lazlo.  I knew I must have been missing some counter
> not being reset.  Both of those options work fine.
> You want to do ifile.seek(0) not reader.seek(0) at least from my testing.
> On Mon, Oct 22, 2012 at 2:27 PM, Matt Williams <mhw at doctors.org.uk> wrote:
>> Dear Rob,
>> This caught me out as well for a long time.
>> As I understand it, csv.reader is a file-reader, which iterates ONCE over
>> the file. There may be more elegant solutions, but I do:
>> import csv
>> ifile = open('test.csv', "r")
>> reader = csv.reader(ifile)
>> inData = []
>> for row in reader:
>>         inData.append[row]
>> ifile.close()
>> you can now loop through inData to your heart's desire.
>> HTH,
>> Matt

If you want to learn more about python iterators this is a good place
to start: http://docs.python.org/howto/functional.html#iterators
or google python iterators.  They are pretty cool to understand
Joel Goldstick

More information about the Tutor mailing list