how to read csv file which has :quote_char in data field

Discussion in 'Ruby' started by Salil Gaikwad, Mar 2, 2009.

  1. How to do this using fastercsv?

    i want to read data from a csv file and save it into the database.
    my file xyz.csv contains following line
    ,,,,"xyz",,,,
    i want o/p of above row
    as follows
    nil,nil,nil,nil,"\"xyz\"", nil,nil,nil,nil

    currently i get
    nil,nil,nil,nil,"\"xyz\"",""

    i am using following method to read the data
    @parsed_file=FasterCSV.read("xyz.csv", :col_sep =>",",
    :quote_char=>',', :force_quotes => true)

    Regards
    Salil
    --
    Posted via http://www.ruby-forum.com/.
    Salil Gaikwad, Mar 2, 2009
    #1
    1. Advertising

  2. Salil Gaikwad

    James Gray Guest

    On Mar 2, 2009, at 9:46 AM, Salil Gaikwad wrote:

    > How to do this using fastercsv?


    It's not really a good for for FasterCSV for the same reason's I
    explained in this message:

    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/329678

    > i want to read data from a csv file and save it into the database.
    > my file xyz.csv contains following line
    > ,,,,"xyz",,,,
    > i want o/p of above row
    > as follows
    > nil,nil,nil,nil,"\"xyz\"", nil,nil,nil,nil
    >
    > currently i get
    > nil,nil,nil,nil,"\"xyz\"",""


    This seems to work:

    >> %Q{,,,,"xyz",,,,}.split(",", -1).map { |f| f.empty? ? nil : f }

    => [nil, nil, nil, nil, "\"xyz\"", nil, nil, nil, nil]

    > i am using following method to read the data
    > @parsed_file=FasterCSV.read("xyz.csv", :col_sep =>",",
    > :quote_char=>',', :force_quotes => true)


    Again, do not set :quote_char and :col_sep to the same value. That's
    impossible to parse. Also :force_quotes is only used when writing
    CSV, not reading it.

    James Edward Gray II
    James Gray, Mar 2, 2009
    #2
    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. Tintin92
    Replies:
    1
    Views:
    1,672
    Andrew Thompson
    Feb 14, 2007
  2. jliu66
    Replies:
    0
    Views:
    492
    jliu66
    Oct 19, 2007
  3. sso
    Replies:
    20
    Views:
    2,608
    Martin Gregorie
    Apr 26, 2009
  4. Li Chen
    Replies:
    18
    Views:
    642
    Azmi Farih
    Mar 23, 2010
  5. jr
    Replies:
    3
    Views:
    402
Loading...

Share This Page