Confused with csv.reader copies

Jeethu Rao jeethu at jeethurao.com
Tue Jul 24 07:51:21 CEST 2007


Robert Dailey wrote:
> First, take a look at my example code:
> -----------------------------------------------------
> import csv
>
> def pass1( reader ):
> 	print reader.next()
> 	print reader.next()
>
> def pass2( reader ):
> 	print reader.next()
> 	print reader.next()
>
> reader = csv.reader( open( "C:/IT/Method/SpaceImpact/code/tools/
> ProfileViewer/performance_profile.csv", "rb" ) )
>
> pass1( reader )
> pass2( reader )
> -----------------------------------------------------
>   
Like Gabriel suggested, using itertools.tee should do the trick.

-----------------------------------------------------

import csv
from itertools import tee

def pass1( reader ):
	print reader.next()
	print reader.next()

def pass2( reader ):
	print reader.next()
	print reader.next()

reader1,reader2 = tee( csv.reader( open( "C:/IT/Method/SpaceImpact/code/tools/
ProfileViewer/performance_profile.csv", "rb" ) ) )

pass1( reader1 )
pass2( reader2 )
-----------------------------------------------------


~ Jeethu



More information about the Python-list mailing list