csv iterator question
Ethan Furman
efurman at admailinc.com
Fri May 23 19:08:54 EDT 2008
davidj411 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()
>
>
>>> fhandle = open('c:/temp/08-02024.csv').readlines()
>>> type(fhandle)
<type 'list'>
>>> len(fhandle)
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)
<type 'list'>
>>> len(contents)
381
You still get a list that you can easily use, that has gone through the
csv routines.
Hope this helps.
--
Ethan
More information about the Python-list
mailing list