# Help with FasterCSV monkey patch

**URL:** <https://www.ruby-forum.com/t/help-with-fastercsv-monkey-patch/158310>\
**Category:** Ruby\
**Created:** [January 21, 2009, 10:51pm UTC](https://www.ruby-forum.com/t/help-with-fastercsv-monkey-patch/158310 "2009-01-21T22:51:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfredmart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a87d85/32.png) [@alfredmart](https://www.ruby-forum.com/u/alfredmart)\
**Post date:** [January 21, 2009, 10:51pm UTC](https://www.ruby-forum.com/t/help-with-fastercsv-monkey-patch/158310/1 "2009-01-21T22:51:57Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![alfredmart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a87d85/32.png) [@alfredmart](https://www.ruby-forum.com/u/alfredmart)\
**Post date:** [January 21, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/help-with-fastercsv-monkey-patch/158310/2 "2009-01-21T23:09:31Z")

</div>

On Wed, Jan 21, 2009 at 4:50 PM, Alfredo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![alfredmart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a87d85/32.png) [@alfredmart](https://www.ruby-forum.com/u/alfredmart)\
**Post date:** [January 21, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/help-with-fastercsv-monkey-patch/158310/3 "2009-01-21T23:18:20Z")

</div>

Gregory B. 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.

---

<div class="post-metadata">

**Author:** ![alfredmart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a87d85/32.png) [@alfredmart](https://www.ruby-forum.com/u/alfredmart)\
**Post date:** [January 22, 2009, 1:36am UTC](https://www.ruby-forum.com/t/help-with-fastercsv-monkey-patch/158310/4 "2009-01-22T01:36:27Z")

</div>

On Jan 21, 2009, at 3:50 PM, Alfredo M. 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 G. II
