to write set of values to a file from python
steve at holdenweb.com
Fri Dec 16 08:13:41 CET 2005
muttu2244 at yahoo.com wrote:
> thanks every body for the help.
> Now how can check whtr the row am reading is the last row or not??
> for example for this below data in csv file
> file = open ('C:\some.csv','r')
> reader = csv.reader(file)
> for row in reader:
> print row
> HERE HOW CAN I CHECK WHTR THIS ROW IS THE LAST ONE IN THE FILE
Don't do that!
> so that if at all i dint find what am searching for i can write that
> information at the last row, after opening the file in a "append" mode.
What you should do is use the "else" clause on the for statement, which
is only executed if the loop terminates normally; something like:
for row in reader:
if row is the one you want:
add new row
Of course, this presumes you don;t want to just print out everything
that's in the file.
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
More information about the Python-list