Forum: Ruby Re: Q about the FasterCSV

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Peña, Botp (Guest)
on 2006-04-28 09:31
(Received via mailing list)
Gregory B. [mailto:removed_email_address@domain.invalid] :

# I think both CSV and FasterCSV can do this.  Same rules apply though,
# just different delimiters.

great! thanks, Greg.

kind regards -botp
Eric L. (Guest)
on 2006-05-09 08:44
(Received via mailing list)
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:in
`shift'
    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:in
`read'
    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>
unknown (Guest)
on 2006-05-09 08:53
(Received via mailing list)
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
Dave B. (Guest)
on 2006-05-09 09:06
(Received via mailing list)
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
Eric L. (Guest)
on 2006-05-09 12:17
(Received via mailing list)
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
This topic is locked and can not be replied to.