CSV variable seems to reset
Tim Golden
mail at timgolden.me.uk
Wed Jun 18 06:24:44 EDT 2008
marc wyburn wrote:
> Hi, I'm using the CSV module to parse a file using
>
> whitelistCSV_file = open("\\pathtoCSV\\whitelist.csv",'rb')
> whitelistCSV = csv.reader(whitelistCSV_file)
> for uname, dname, nname in whitelistCSV:
> print uname, dname, nname
>
> The first time I run the for loop the contents of the file is
> displayed. Subsequent attempts to run the for loop I don't get any
> data back and no exception.
That's because the csv reader object is an iterator. You're
consuming it as you run through it. If you want to keep hold
of the contents to run again (without reinitialising it) then
pull the data into a list and iterate over that as many times
as you like:
data = list (whitelistCSV)
for uname, dname, nname in data:
# do stuff
for uname, dname, nname in data:
# do stuff again
TJG
More information about the Python-list
mailing list