[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Changing the quote-character in csv parsing

Jens Auer

3/28/2006 3:02:00 PM

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'"]

I already have searched the rdoc for the csv library without any success.
3 Answers

Jan Topinski

3/28/2006 4:04:00 PM

0

Hi,
Don't know how it is with fastercsv but csv.rb has double quote hardcoded as
I understand. I think best is to substitute all double quots in your text
with single and vice versa. You can do it with gsub this way:


line.gsub!(/'|\"/){ |c|
if c == "'"
"\""
else
"'"
end
}

jan

Jens Auer wrote:

> 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'"]
>
> I already have searched the rdoc for the csv library without any success.

William James

3/28/2006 4:08:00 PM

0

Jan Topinski wrote:
> Hi,
> Don't know how it is with fastercsv but csv.rb has double quote hardcoded as
> I understand. I think best is to substitute all double quots in your text
> with single and vice versa. You can do it with gsub this way:
>
>
> line.gsub!(/'|\"/){ |c|
> if c == "'"
> "\""
> else
> "'"
> end
> }

"He said, \"I don't care.\"".tr("'\"", "\"'")

Jan Topinski

3/28/2006 4:18:00 PM

0

ups me blind ;)
jan
William James wrote:

> Jan Topinski wrote:
>> Hi,
>> Don't know how it is with fastercsv but csv.rb has double quote hardcoded
>> as I understand. I think best is to substitute all double quots in your
>> text with single and vice versa. You can do it with gsub this way:
>>
>>
>> line.gsub!(/'|\"/){ |c|
>> if c == "'"
>> "\""
>> else
>> "'"
>> end
>> }
>
> "He said, \"I don't care.\"".tr("'\"", "\"'")