reading csv problem

A

ajikoe

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
 
C

Cameron Walsh

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.
 
A

ajikoe

Thank you.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top