Re: FasterCSV: preserving quoted strings
On Feb 26, 2009, at 8:14 AM, BIl Kleb wrote:
> Google et al are failing me: How do I preserve quoted
> CSV strings on output?
I'm not totally sure I understand the question, but your test made it
look like your data was one field you wanted to be able to read in.
If so, you'll need to write is out properly escaped first:
require 'rubygems'
require 'faster_csv'
require 'test/unit'
class ConversionTest < Test::Unit::TestCase
def test_preserve_quoted_strings
field = '"string",2,0.3'
csv = [field].to_csv # => "\"\"\"string\"\",2,0.3\"\n"
assert_equal(field, csv.parse_csv.first)
end
end
If I'm wrong and you meant for that to be three separate fields, then
just bust them up to get the valid CSV:
require 'rubygems'
require 'faster_csv'
require 'test/unit'
class ConversionTest < Test::Unit::TestCase
def test_preserve_quoted_strings
fields = '"string",2,0.3'.split(",")
csv = fields.to_csv # => "\"\"\"string\"\"\",2,0.3\n"
assert_equal(fields, csv.parse_csv)
end
end
Hope that helps.
James Edward Gray II