csv iterator question

D

davidj411

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:.... 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?
.... print line
....

do i need to seek() the beginning of this file object ? any help is
greatly appreciated here.
 
M

Mike Driscoll

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'))

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


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

Marc 'BlackJack' Rintsch

example of open method on file object:
fhandle=open(filename).readelines()

Here the name `fhandle` is somewhat misleading. You don't bind the file
object to that name, but the result of the call of the `readlines()`
method. Which is a list of lines. If you would have bound the file
object, you'd observe similar behavior to your CSV reader example.
example of csv.reader method:
reader = csv.reader(open(csvfilename))

Because here you are binding the reader object. Throw in a `list()` to
read all CSV records into a list:

records = list(csv.reader(open(csv_filename)))

In both cases you don't close the file explicitly which might cause
problems. That CPython closes it for you almost immediately is an
implementation detail. Other implementations might let those open files
stay open for much longer.

Ciao,
Marc 'BlackJack' Rintsch
 
E

Ethan Furman

davidj411 said:
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()
381

fhandle is a list containing the contents of the file, not a file handle
-- that's why you can easily re-use it.
example of csv.reader method:
reader = csv.reader(open(csvfilename))

Try this instead:
>>>reader = csv.reader(open('c:/temp/08-02024.csv'))
>>> contents = [line for line in reader]
>>> type(contents)
>>> len(contents)
381

You still get a list that you can easily use, that has gone through the
csv routines.

Hope this helps.
 
A

alex23

but when i try to iterate through it again, it appears to print
nothing (no error either). the file is exhausted?

No, the iterator is finished. Iterators are generally use-once:

"The intention of the protocol is that once an iterator's next()
method raises StopIteration, it will continue to do so on subsequent
calls. Implementations that do not obey this property are deemed
broken." [http://docs.python.org/lib/typeiter.html]

As well as reading the iterator into a list, you can also create a
copy of the iterator:

Two copies is the default, but you can optionally specify as many as
you like:

- alex23
 

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

Forum statistics

Threads
474,241
Messages
2,571,219
Members
47,851
Latest member
TheoChestn

Latest Threads

Top