to write set of values to a file from python

Steve Holden steve at holdenweb.com
Fri Dec 16 08:13:41 CET 2005


muttu2244 at yahoo.com wrote:
> hi
> 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
> 
> CompName,IpAddr,MacAddr
>  XXX,192.178.23.11,78.23.34.23.12
>  YYY,192.189.22.11,89.23.43.12.34
>  ZZZ,192.179.24.45,89.23.34.12.45
> 
> 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:
         break
else:
     add new row

Of course, this presumes you don;t want to just print out everything 
that's in the file.

regards
  Steve
-- 
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 mailing list