473,387 Members | 1,891 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,387 software developers and data experts.

CSV variable seems to reset

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.

The only way I can get the data is to run whitelistCSV_file = open("\
\pathtoCSV\\whitelist.csv",'rb') again.

I'm stumped as to how I can get around this or work out what the
problem is, I'm assuming that the 'open' command buffers the data
somewhere and that data is being wiped out by the CSV module but
surely this shouldn't happen.

thanks, Marc.
Jun 27 '08 #1
1 1029
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
Jun 27 '08 #2

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

Similar topics

8
by: 2L | last post by:
Trying to write some code and this is just shitting me now! Bit of form validation stuff.... Variable is declared as follows..... --snip-- <script language="JavaScript"> <!---
1
by: farooqazeem | last post by:
Hi guys, I’m facing some problem can u solve it. Problem is: I’m giving user Id and password in (Login_sess.asp) and submit it to page (sess_test.asp). I am setting session variable...
8
by: Mike Turco | last post by:
This is a strange one. I have an app that needs to know who is using the program but there's no need for security. When the program opens a form comes up with a listbox. The user double-clicks...
4
by: N. Demos | last post by:
Hello, I'm learning ASP.NET, and am having a strange problem with some example code from the book I'm using. The code increments and displays the value stored in a session variable when the "Add"...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
8
by: Phoebe. | last post by:
Hi, Good Day! I need to create a variable that it's value can be carry across within a form. I've created some above the "web form designer generated code" but it seems not working. It...
4
by: Boni | last post by:
Dear all, how can I initialize shared variable? i.e class A private shared B as boolean end class in C++ I would write in the global scope A::B=true What is the proper way in vb
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
5
by: xirowei | last post by:
i'm newbie in java servlet, how to let public void doPost can access to public void doGet, stringLength variable? below is my code: import javax.servlet.*; import javax.servlet.http.*; import...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.