Re: Q about the FasterCSV

Gregory B. [mailto:[email protected]] :

I think both CSV and FasterCSV can do this. Same rules apply though,

just different delimiters.

great! thanks, Greg.

kind regards -botp

Why ‘a,"bc"d’ could not be parsed correctly in both FasterCSV and CSV
libary. while
“a,'bc’d” could be parsed into [[“a”, “'bc’d”]] successfully.

irb(main):001:0> require ‘fastercsv’
true
irb(main):002:0> FasterCSV.parse(“a,'bc’d”)
[[“a”, “'bc’d”]]
irb(main):003:0> FasterCSV.parse(‘a,"bc"d’)
FasterCSV::MalformedCSVError: Unclosed quoted field on line 1.
from
e:/ruby/lib/ruby/gems/1.8/gems/fastercsv-0.2.0/lib/faster_csv.rb:1165:in
shift' from e:/ruby/lib/ruby/gems/1.8/gems/fastercsv-0.2.0/lib/faster_csv.rb:1090:inshift’
from
e:/ruby/lib/ruby/gems/1.8/gems/fastercsv-0.2.0/lib/faster_csv.rb:1040:in
each' from e:/ruby/lib/ruby/gems/1.8/gems/fastercsv-0.2.0/lib/faster_csv.rb:1051:inread’
from
e:/ruby/lib/ruby/gems/1.8/gems/fastercsv-0.2.0/lib/faster_csv.rb:815:in
`parse’
from (irb):3
irb(main):004:0>

On Tue, 9 May 2006, Eric L. wrote:

Why ‘a,"bc"d’ could not be parsed correctly in both FasterCSV and CSV
libary. while
“a,'bc’d” could be parsed into [[“a”, “'bc’d”]] successfully.

it’s malformed. it should look like this:

jib:~ > ruby -r csv -e’ CSV::Writer::generate(STDOUT){|c| c << %w[ a
“bc"d ]} ’
a,”"“bc”“d”

“a,'bc’d” is correct

jib:~ > ruby -r csv -e" CSV::Writer::generate(STDOUT){|c| c << %w[ a
'bc’d ]} "
a,'bc’d

double quotes must be escaped if they are to be within a field.

regards.

-a

Eric L. wrote:

Why ‘a,"bc"d’ could not be parsed correctly in both FasterCSV and CSV
libary. while
“a,'bc’d” could be parsed into [[“a”, “'bc’d”]] successfully.

irb(main):001:0> require ‘fastercsv’
true
irb(main):002:0> FasterCSV.parse(“a,'bc’d”)
[[“a”, “'bc’d”]]
irb(main):003:0> FasterCSV.parse(‘a,"bc"d’)
FasterCSV::MalformedCSVError: Unclosed quoted field on line 1.

Here are the rules:

  1. Commas (,) separate fields. (You can pick a different delimiter if
    you like.)
  2. Double-quotes (") are used to quote fields. They must surround the
    entire field.
  3. Double-quotes (") are used to escape double-quotes inside a field.

If you want to write [‘a’, ‘"bc"d’] as CSV, do this:
a,""“bc”“d”

Cheers,
Dave

Thanks for your patient and time, Dave

Finally I understand the rules of the CSV format by your clear
explanatioon.
I really appreciate your help