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
Gregory B. [mailto:[email protected]] :
great! thanks, Greg.
kind regards -botp
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>
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:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs