csv iterator question

Mike Driscoll kyosohma at gmail.com
Fri May 23 16:53:28 EDT 2008


On May 23, 3:36 pm, davidj411 <davidj... at gmail.com> wrote:
> When you save an open file to a variable, you can re-use that variable
> for membership checking.
> it does not seem to be that way with the csv.reader function, even
> when set to a variable name.
>
> what is the best way to store the open CSV file in memory or do i need
> to open the file each time?
>
> example of open method on file object:
> fhandle=open(filename).readelines()
>
> example of csv.reader method:
> reader = csv.reader(open(csvfilename))
>
> here are my results:>>> reader = csv.reader(open('export.csv'))
> >>> for line in reader:
>
> ...  print line
>
> <lots of data on page...>
>
> but when i try to iterate through it again, it appears to print
> nothing (no error either). the file is exhausted?
>
> >>> for line in reader:
>
> ...  print line
> ...
>
> do i need to seek() the beginning of this file object ? any help is
> greatly appreciated here.

I think using seek() is what you need as it is currently starting on
the last line that it read, so to speak. You should have just tried
it...experimenting is part of the fun of Python, after all.

Mike



More information about the Python-list mailing list