[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Help with FasterCSV monkey patch

Alfredo Mesen

1/21/2009 9:50:00 PM

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 :)
--
Posted via http://www.ruby-....

3 Answers

Gregory Brown

1/21/2009 10:07:00 PM

0

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

--
Technical Blaag at: http://blog.majesticseacr...
Non-tech stuff at: http://metametta.bl...
"Ruby Best Practices" Book now in O'Reilly Roughcuts:
http://rubybestpra...

Alfredo Mesen

1/21/2009 10:17:00 PM

0

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.

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

James Gray

1/22/2009 12:34:00 AM

0

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