doubt on csv file

M

muttu2244

hi all

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

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.

thanks for the help
shiva
 
D

Dan M

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"
else:
f.close()
f = open("c:\\some.csv", 'a')
f.write("The new row data")
f.close()

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.

HTH
 
S

skip

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,052
Latest member
EvaW192252

Latest Threads

Top