By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

reading csv problem

P: n/a
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

Nov 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
aj****@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.
Nov 15 '06 #2

P: n/a
Thank you.
On Nov 15, 1:33 pm, Cameron Walsh <cameron.wa...@gmail.comwrote:
aji...@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,
ajikoeBecause 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.
Nov 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.