shiva> am trying to search whtr my string is found in a csv file, if its
    shiva> not found i have to append it at the last row of a csv file, How
    shiva> can i do that??

Given that you specified a csv file, I presume you want to maintain the
proper semantics.  You clearly know how to read it, though beware that you
open the file in binary mode and use raw strings to preserve the literal
backslash in your file spec:

    csvfile = open (r'C:\some.csv','rb')

The rows in the reader will be returned as lists of strings, so search for
your field value as you would any other list of strings:

    interesting_value = "XYZ"
    reader = csv.reader(csvfile)
    found = False
    for row in reader:
        found = interesting_value in row:
        if found:
            break
    csvfile.close()

If your interesting value wasn't found, you need to write a new row to the
file.  Again, you probably want to maintain your csv file's structure, so
instead of blindly appending a line at the end (reopen in append mode),
build a row then write it:

    csvfile = open (r'C:\some.csv','ab')
    newrow = [..., interesting_value, ...]
    writer = csv.writer(csvfile)
    writer.writerow(newrow)
    csvfile.close()    
    
You didn't give any details on the structure of your csv file, so the
construction of newrow is purposely fuzzy.

Skip
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to