473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

csv iterator question

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.
Jun 27 '08 #1
4 1279
On May 23, 3:36*pm, davidj411 <davidj...@gmail.comwrote:
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
Jun 27 '08 #2
On Fri, 23 May 2008 13:36:55 -0700, davidj411 wrote:
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
Jun 27 '08 #3
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

Jun 27 '08 #4
On May 24, 6:36 am, davidj411 <davidj...@gmail.comwrote:
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:
>>import itertools
reader1, reader2 = itertools.tee(csv.reader(open('export.csv')))
Two copies is the default, but you can optionally specify as many as
you like:
>>m, n, o = itertools.tee(iterator, 3)
- alex23


Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
5
by: music4 | last post by:
Greetings, I want to STL map class. But I don't know how to use iterator. Let me state my question by following small sample: map<int, int> mymap; // insert some <key,value> in mymap ...
11
by: Mateusz Loskot | last post by:
Hi, I have a simple question about naming convention, recommeded names or so in following case. I have a class which is implemented with one of STL container (aggregated) and my aim is to...
8
by: Mateusz Ɓoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
5
by: Mark Stijnman | last post by:
I have a question about forward iterators and what one should do or not do with them. I'm planning on writing a container that, when all boils down to it, stores a sequence of strings. I want...
3
by: wolverine | last post by:
Hi I am accessing a map from inside threads. There is a chance that an element is inserted into the map, from inside any thread. Since i don't know about thread safety of stl implementation i am...
3
by: Alex__655321 | last post by:
Hello All I hope I'm correct posting an STL question here - if not feel free to direct me to somewhere more appropriate. I'm writing some code using an std::set which I believe is the best...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.