addin lines to existing csv file with fastercsv

Discussion in 'Ruby' started by Mark Stroeve, May 18, 2007.

  1. Mark Stroeve

    Mark Stroeve Guest

    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-forum.com/.
    Mark Stroeve, May 18, 2007
    #1
    1. Advertising

  2. > 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-forum.com/.
    >
    >
    Stephen Smith, May 18, 2007
    #2
    1. Advertising

  3. 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.
    Brian Candler, May 18, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Edward Gray II
    Replies:
    0
    Views:
    90
    James Edward Gray II
    Nov 16, 2005
  2. Wes Gamble
    Replies:
    1
    Views:
    105
    Wes Gamble
    Dec 18, 2006
  3. Li Chen
    Replies:
    10
    Views:
    199
    Rob Biedenharn
    Sep 24, 2008
  4. John Mcleod
    Replies:
    1
    Views:
    157
    John Mcleod
    Jul 16, 2009
  5. John Mcleod

    using FasterCSV to clean CSV file

    John Mcleod, Nov 17, 2009, in forum: Ruby
    Replies:
    7
    Views:
    169
    James Edward Gray II
    Nov 18, 2009
Loading...

Share This Page