doubt on csv file

Dan M dan at
Fri Dec 16 00:12:28 CET 2005

> here is what am trying to do, first am trying to open it in a read
> mode, and checking each row by row , if it is not found till my last
> row, i want to append it to the last row , so how should i do that.
> file = open ('C:\some.csv','r')
> reader = csv.reader(file)
> for row in reader:
>     print row
> How can i update at the last row.

This newbie's idea is that you'd want to keep track of whether you'd found
the item you are looking for. If not found, close the file, reopen it
in append mode, and write the new row. For example:
 f = open ('C:\some.csv','r')
 rdr = csv.reader(file)
 found = 0
 for row in rdr:
     if row[0] == 'YYY': found = 1
     print row
 if found:
     print "No need to do anything more"
     f = open("c:\\some.csv", 'a')
     f.write("The new row data")

I am admittedly a newbie; I'm sure more experienced users will have more
efficient suggestions. Note that I did change "file" to f to avoid hiding
the "file" method and changed "reader" to rdr for the same reason. 


More information about the Python-list mailing list