Forum: Ruby Help with FasterCSV monkey patch

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.
664570a7fab31d289062c87adfc8daea?d=identicon&s=25 Alfredo Mesen (albemuth)
on 2009-01-21 22:51
Hello, I've been trying to monkey patch FCSV to allow escaped colsep
characters within a field:

FCSV.parse "here\\, it is, other fields"  #need:  [["here\\, it is",
"other fields"]]
                 ^

So what I tried was modifying the :csv_row regexp on the init_parsers
method


1797:           ([^#{esc_quote}#{esc_col_sep}]*)   # unquoted fields


which I changed with this:

1797:           ((?>[^#{esc_quote}#{esc_col_sep}]*
1798:           \\#{esc_col_sep}
1799:           [^#{esc_quote}#{esc_col_sep}]*)*)   # unquoted fields

This didn't work though :(, though my gut tells me I'm close ;)

Thanks in advance for any help with this :)
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-01-21 23:09
(Received via mailing list)
On Wed, Jan 21, 2009 at 4:50 PM, Alfredo Mesen <albemuth@gmail.com>
wrote:
> Hello, I've been trying to monkey patch FCSV to allow escaped colsep
> characters within a field:
>
> FCSV.parse "here\\, it is, other fields"  #need:  [["here\\, it is",
> "other fields"]]
>                 ^

Is there a reason why you can't use normal CSV quoted text escaping?

>> FCSV.parse '"here, it is", other fields'
=> [["here, it is", " other fields"]]

-greg
664570a7fab31d289062c87adfc8daea?d=identicon&s=25 Alfredo Mesen (albemuth)
on 2009-01-21 23:18
Gregory Brown wrote:
>
> Is there a reason why you can't use normal CSV quoted text escaping?
>
>>> FCSV.parse '"here, it is", other fields'
> => [["here, it is", " other fields"]]
>
> -greg

Unfortunately yes, the parsing on the app I'm working on has to be
foolproof, allowing such otherwise-invalid format.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-01-22 01:36
(Received via mailing list)
On Jan 21, 2009, at 3:50 PM, Alfredo Mesen wrote:

> Hello, I've been trying to monkey patch FCSV to allow escaped colsep
> characters within a field:
>
> FCSV.parse "here\\, it is, other fields"  #need:  [["here\\, it is",
> "other fields"]]

My advice is don't do this, unfortunately.  :(

I've tried to add this feature to FasterCSV multiple times now.  It's
very hard and I haven't been able to find a good way to do it for
general cases.  I fully admit this is a failing of FasterCSV, it's
very dependent on the proper CSV format.

You probably have three reasonable choices:

* Feed FasterCSV a line at a time, rescue the MalformedCSVError, and
switch strategies on those lines
* Preprocess all lines to be sure they are valid CSV and then hand
them off
* Build your own parser

Sorry I wasn't more help.

James Edward Gray II
This topic is locked and can not be replied to.