From: Charles Calvert Date: 2010-10-27T04:33:06+09:00 Subject: Re: Update a particular Field in a csv file Probably too late to help the OP, but in case anyone finds this in an archive ... On Thu, 15 Apr 2010 08:35:40 -0500, Antony Nambikkai wrote in <9194cbbbcba0148e8db87e3a509aee6b@ruby-forum.com>: >Hi Friends, > >I am having a role.csv file as like > >emp_id,dep_id,role,salary,accepted >1,1,SSE,10000,YES >2,2,SSE,11000,NO >3,1,SSE,9000,YES >4,2,SSE,12000,YES >5,1,SSE,8000,NO > >Now, I need to update this role.csv file as like below > >emp_id,dep_id,role,salary,accepted >1,1,SSE,10000,NO >2,2,SSE,11000,YES >3,1,SSE,9000,NO >4,2,SSE,12000,YES >5,1,SSE,8000,NO [snip code] >I am facing 2 issues in this > >1. For every updating the CSV file, I am rewriting the whole CSV file. >That not a teddy way to do it. >2. I am opening the file first time for reading and 2nd time for >writing. That is also not convincing me. The problem is that you're dealing with a single file, not a database. In order to alter the contents of that file, you're going to have to read the entire thing into memory, alter it, and then write it back out to disk. That's just the way it works. You could always move the data into a database, which would allow you to update only an individual record. -- Charles Calvert Moderator - alt.computer.consultants.moderated Submission Address: accm@celticwolf.net Contact Address: accm_mod@celticwolf.net