473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

doubt on csv file

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

Dec 15 '05 #1
4 1464
> 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
Dec 15 '05 #2
hey thanks a lot for that it worked .

Dec 16 '05 #3
On Thu, 15 Dec 2005 16:09:51 -0800, muttu2244 wrote:
hey thanks a lot for that it worked .


Excellent! Glad to be of assistance :)
Dec 16 '05 #4

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
Dec 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: RK | last post by:
Hi, In my application, I need to copy data from an Excel file into a SQL table. The article related to this can be found at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B306572 ...
6
by: Baskar RajaSekharan | last post by:
In C-sharp, I wnat to know whether the Component is compiled in Debug Mode or Run Mode through Code. How is it possible? Is there any way to Access the Config file and check? Please let me know...
2
by: robertoviperbr | last post by:
Hello to everybody. I have a doubt see the code: #include <vector> #include <iostream> #include <string> #include <algorithm> #include <sstream> #include <fstream> #include <iomanip>
122
by: ivan | last post by:
hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks
2
by: sandhya2006 | last post by:
Hii, I hav a doubt in perl,Assume there is many html files in a folder I have to Take all files and display it into text file without displaying the tags.Please reply atleast for displaying...
1
sivisr
by: sivisr | last post by:
hi every one.... i have a doubt in c#...that is can we read a mp3 format through file stream...wav file is reading.......
2
archulu
by: archulu | last post by:
hai this is archulu, i have a some confusion and doubt in my downloading program.that doubt was in my program i am upload some gif and img file to some path.it's good, after usage of that image i...
1
by: wcl152010 | last post by:
Hi, I have a general doubt here regarding of monitoring different model of CISCO Switches and Routers. I am new in SNMP, I tried to search for CISCO related mib file in the net and i got at...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.