I've come across a behavior in python's built-in csv module that I've never noticed before. Typically, when I read in a csv, it's following the doc's pretty much verbatim, using 'with' to open the file then looping over the reader object with a 'for' loop. However, I recently tried iterating over the csv.reader object twice in a row, only to find out that the second 'for' loop did nothing.
import csv
with open('smallfriends.csv','rU') as csvfile:
readit = csv.reader(csvfile,delimiter=',')
for line in readit:
print line
for line in readit:
print 'foo'
Console Output:
Austins-iMac:Desktop austin$ python -i amy.py
['Amy', 'James', 'Nathan', 'Sara', 'Kayley', 'Alexis']
['James', 'Nathan', 'Tristan', 'Miles', 'Amy', 'Dave']
['Nathan', 'Amy', 'James', 'Tristan', 'Will', 'Zoey']
['Kayley', 'Amy', 'Alexis', 'Mikey', 'Sara', 'Baxter']
>>>
>>> readit
<_csv.reader object at 0x1023fa3d0>
>>>
So the second 'for' loop basically does nothing. One thought I had is the csv.reader object is being released from memory after being read once. This isn't the case though since it still retains it's memory address. I found a post that mentions a similar problem. The reason they gave is that once the object is read, the pointer stay's at the end of the memory address ready to write data to the object. Is this correct? Could someone go into greater detail as to what is going on here? Is there a way to push the pointer back to the beginning of the memory address to reread it? I know it's bad coding practices to do that but I'm mainly just curious and wanting to learn more about what goes on under Python's hood.
Thanks!