[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

addin lines to existing csv file with fastercsv

Mark Stroeve

5/18/2007 12:07:00 AM

I'am trying to add new lines to a existing csv file, but i only get one
line.
It doesn't add additional lines, i pasted te code below, has anyone a
suggestion how to write new lines?


require 'fastercsv'


FasterCSV.open("d:/file.csv", "w") do |csv|
csv << [@params[:ha_gewas2], "first name", "last name", "email"]
end

--
Posted via http://www.ruby-....

2 Answers

Stephen Smith

5/18/2007 12:22:00 AM

0

> It doesn't add additional lines, i pasted te code below, has anyone a
> suggestion how to write new lines?
You need to insert newlines after each record string.

For example:
hdr = [['field1a', 'field1b'], 'field2', 'field3',
'field4'].flatten.join(',')
FasterCSV.open("my_csv_file.csv", "w") {|file|
file << hdr + "\n" # There's a different newline
thingamajigger for Windows: \r\n
# proceed with adding additional records here.
}
>
>
> require 'fastercsv'
>
>
> FasterCSV.open("d:/file.csv", "w") do |csv|
> csv << [@params[:ha_gewas2], "first name", "last name", "email"]
> end
>
> --
> Posted via http://www.ruby-....
>
>

Brian Candler

5/18/2007 5:10:00 AM

0

On Fri, May 18, 2007 at 09:06:39AM +0900, Mark Stroeve wrote:
> I'am trying to add new lines to a existing csv file, but i only get one
> line.
> It doesn't add additional lines, i pasted te code below, has anyone a
> suggestion how to write new lines?
>
>
> require 'fastercsv'
>
>
> FasterCSV.open("d:/file.csv", "w") do |csv|
> csv << [@params[:ha_gewas2], "first name", "last name", "email"]
> end

Open using "a" instead of "w" to append to the file. See "ri IO" for a list
of file modes.