Re: [Tutor] newb help reading lines from csv

2012-10-26 Thread Matt Williams

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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Prasad, Ramit
Dewhirst, Rob 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  wrote:
[snip]

Please do not top post. This list's etiquette is to write your 
response in-line or below the quoted text. Thank you.

Ramit Prasad

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread eryksun
On Mon, Oct 22, 2012 at 3:35 PM, Dewhirst, Rob  wrote:
>
>> 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.

Alternatively:

import csv
with open('test.csv', 'rb') as ifile:
inData = list(csv.reader(ifile))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Joel Goldstick
On Mon, Oct 22, 2012 at 3:35 PM, Dewhirst, Rob  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  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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread eryksun
On Mon, Oct 22, 2012 at 3:28 PM, LZAntal  wrote:
> On Oct 22, 2012, at 12:20 PM, "Dewhirst, Rob"  wrote:
>
>> import csv
>> ifile = open('test.csv', "r")
>> reader = csv.reader(ifile)
>
> I believe csv module uses iterator so you need to run reader.seek(0) between 
> the for loops

You have to reset the file iterator with ifile.seek(0). You might also
want a new reader if you care about the line_num attribute, but
otherwise you can keep using the same reader.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Dewhirst, Rob
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  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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Joel Goldstick
On Mon, Oct 22, 2012 at 3:20 PM, Dewhirst, Rob  wrote:
> import csv
> ifile = open('test.csv', "r")
> reader = csv.reader(ifile)
> for row in reader:
> print row
> for row in reader:
> print row
> ifile.close()
>
> This is a simplified version of what I am trying to do - loop through
> a CSV file twice.  Why does the second for loop not execute at all?
> The first one prints the rows of the file just fine.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

You need to close the file and re open to iterate again.  You can't
reset the iterator back to the beginning without closing and
re-opening

-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread Emile van Sebille

Dewhirst, Rob wrote:

import csv
ifile = open('test.csv', "r")
reader = csv.reader(ifile)
for row in reader:
print row
for row in reader:
print row
ifile.close()

This is a simplified version of what I am trying to do - loop through
a CSV file twice.  Why does the second for loop not execute at all?
The first one prints the rows of the file just fine.


The first pass also exhausts the input feed -- you'll need to rewind or 
reposition the next line pointer to the start of the file and I suspect 
the easiest way is to reopen the file.


Emile

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newb help reading lines from csv

2012-10-22 Thread LZAntal
Hi,

On Oct 22, 2012, at 12:20 PM, "Dewhirst, Rob"  wrote:

> import csv
> ifile = open('test.csv', "r")
> reader = csv.reader(ifile)
> for row in reader:
>   print row
> for row in reader:
>   print row
> ifile.close()
> 
> This is a simplified version of what I am trying to do - loop through
> a CSV file twice.  Why does the second for loop not execute at all?
> The first one prints the rows of the file just fine.
> _

I believe csv module uses iterator so you need to run reader.seek(0) between 
the for loops


Laszlo
http://twitter.com/LZAntal


> __
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] newb help reading lines from csv

2012-10-22 Thread Dewhirst, Rob
import csv
ifile = open('test.csv', "r")
reader = csv.reader(ifile)
for row in reader:
print row
for row in reader:
print row
ifile.close()

This is a simplified version of what I am trying to do - loop through
a CSV file twice.  Why does the second for loop not execute at all?
The first one prints the rows of the file just fine.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor