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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C42c706fab23da0b6d3cd2cfbb8db27d?d=identicon&s=25 Salil Gaikwad (salil)
on 2009-03-02 16:47
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-03-02 17:11
(Received via mailing list)
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/...

> 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
This topic is locked and can not be replied to.