reading csv problem

Cameron Walsh cameron.walsh at gmail.com
Wed Nov 15 07:33:15 CET 2006


ajikoe at gmail.com wrote:
> Hello,
> 
> I use csv to take information from file.
> import csv
> reader = csv.reader(open('t.csv'))
> 
> for row in reader:
>     print row # it is perfectly OK
> 
> ---------------------------------------------------------------------
> But If I use this code I have problem
> import csv
> reader = csv.reader(open('t.csv'))
> 
> for row in reader:
>     print row # it is perfectly OK
> for row in reader:
>     print row # it is not printed on the monitor???
> 
> Why does only the first print row work here?
> 
> TIA,
> ajikoe
> 

Because reader is an iterator and has no means of going back to its
beginning.

You could make a list out of it:
lines = list(csv.reader(open('t.csv')))
for row in lines:
  print row
for row in lines:
  print row

Hope it helps,

Cameron.



More information about the Python-list mailing list