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.
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2006-04-28 07:31
(Received via mailing list)
Gregory Brown [mailto:gregory.t.brown@gmail.com] :

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

great! thanks, Greg.

kind regards -botp
F1c441ba5569ec34d0613fe8c218e076?d=identicon&s=25 Eric Luo (Guest)
on 2006-05-09 06: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>
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-05-09 06:53
(Received via mailing list)
On Tue, 9 May 2006, Eric Luo 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
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-05-09 07:06
(Received via mailing list)
Eric Luo 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
F1c441ba5569ec34d0613fe8c218e076?d=identicon&s=25 Eric Luo (Guest)
on 2006-05-09 10: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.