csv iterator question

David Jackson davidj411 at gmail.com
Sat May 24 14:13:12 EDT 2008


Thanks, Ethan. that was a great solution. i just tested it.


On Fri, May 23, 2008 at 7:08 PM, Ethan Furman <efurman at admailinc.com> wrote:

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080524/91d3ced5/attachment-0001.html>


More information about the Python-list mailing list