From: bbiker Date: 2006-07-16T10:10:10+09:00 Subject: Re: Appending to a CSV file gregarican wrote: > I can't speak for the OP, but I am assuming that they were using the > CSV module because there might have been some complications in > processing the data source. And perhaps some of the CSV module's > methods helped streamline things so they didn't have to reinvent the > wheel. > > No matter if you are using the CSV module or rolling your own way of > processing a data source file, you would have to parse through the > existing data in some block form and then append to the end of the > stream/array/whatever to accomplish what the OP was looking to do. > > If you post a code snippet of your way to accomplishing this without > using the CSV module I would think that the basic concept and workflow > of the script would be similar... > As I said I am a newby to Ruby but not to programming in general. Now to me a csv file is simply a flatfile database stored in a text file. The only specifications are that fields have a separator (normally a comma) and the records have a separator (normally a newline). In addition neither of these separators can appear within a field. So to me, I only need to read a record and write a record. Once a record is read, how I slice or dice the record does not require a csv module that provides unique functions to process the record. Remember a csv record is a string, period. There are no other structural requirements. Now it is up to me to verify the sanity of the records and believe me no module can be written to cover all possiblities since the records are free-form and can have any format what so ever. Usually, I open the csv file, read in the records into an array, close the file and then process the records as appropriately. Should I need to write those records back I either re-open the csv file in write mode (which clobbers the original file) or open a newfile in write mode. Write out each record. If I want to keep the original file as a back up, I can rename it and give the new file the old name. If my program generates new records that need to be added to an existing file, I open the file in append mode and writeout the record(s). I normally do not use csv files as a permanent database. They usually are "pass-through" files..an external program may write it outputs in a csv file which my program processes and/or my program may store its outputs in a csv file for another program to use.