# Chaning the quote-character in csv parsing

**URL:** <https://www.ruby-forum.com/t/chaning-the-quote-character-in-csv-parsing/89069>\
**Category:** Ruby\
**Created:** [March 28, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/chaning-the-quote-character-in-csv-parsing/89069 "2006-03-28T16:04:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![snej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@snej](https://www.ruby-forum.com/u/snej)\
**Post date:** [March 28, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/chaning-the-quote-character-in-csv-parsing/89069/1 "2006-03-28T16:04:16Z")

</div>

Hi,  
I have a bunch of files containing lines as comma-seperated values.  
Unfortunately, the character used for quoting is a single quote (’) and  
not the double quote ("). How can I tell the csv library (or fastercsv  
or any other csv-parsing library) which character is used for quoting?  
Some of the files contain fields like ‘quoted, but with comma’, which  
are seperated into two fields at the comma:  
irb(main):003:0\> line = “one, ‘quoted’, ‘quoted, but with comma’”  
=\> “one, ‘quoted’, ‘quoted, but with comma’”  
irb(main):006:0\> CSV::parse\_line(‘some words “some quoted text” some  
more words’, ’ ')  
=\> [“some”, “words”, “some quoted text”, “some”, “more”, “words”]  
irb(main):001:0\> require ‘rubygems’  
=\> true  
irb(main):002:0\> require\_gem ‘fastercsv’  
=\> true  
irb(main):004:0\> line.parse\_csv  
=\> [“one”, " ‘quoted’", " ‘quoted", " but with comma’"]

The output should be [“one”, “‘quoted’”, “‘quoted, but with comma’”]

---

<div class="post-metadata">

**Author:** ![snej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@snej](https://www.ruby-forum.com/u/snej)\
**Post date:** [March 28, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/chaning-the-quote-character-in-csv-parsing/89069/2 "2006-03-28T18:04:18Z")

</div>

On Mar 28, 2006, at 8:03 AM, Jens A. wrote:

> I have a bunch of files containing lines as comma-seperated values.  
> Unfortunately, the character used for quoting is a single quote (’)  
> and not the double quote ("). How can I tell the csv library (or  
> fastercsv or any other csv-parsing library) which character is used  
> for quoting?

I’m not aware of a way to change it for either library, sadly. If  
your data is simple, you might get away with manipulating it into shape:

> > require “csv”  
> > =\> true
> 
> > line = “one,‘quoted’,‘quoted, but with comma’”  
> > =\> “one,‘quoted’,‘quoted, but with comma’”
> 
> > CSV.parse\_line(line.tr("’", ‘"’))  
> > =\> [“one”, “quoted”, “quoted, but with comma”]

Unfortunately, on of the examples you showed is more complicated than  
that:

> > line = ‘some words “some quoted text” some more words’  
> > =\> “some words “some quoted text” some more words”
> 
> > CSV.parse\_line(’"’ + line.gsub(’"’, ‘""’).tr("’", ‘"’) + ‘"’)  
> > =\> [“some words “some quoted text” some more words”]

Those transforms need to be done on a field by field basis though, to  
correctly convert data like that.

Hope that helps.

James Edward G. II
