Ruby CSV - Trying to wrap output in double quotes, getting """Hello World""" instead of "Hello World"
csv, ruby
Solution
This is correct. Quote characters are escaped in CSV files by doubling. And all fields that contain commas, newlines and/or quote characters need to be enclosed in quotes.
So the first quote starts a quoted field, the second and third quote encode the actual quote character.
becomes
Hello,"Field, with comma","2"" by 4""",123
Problem
``` require 'csv' s = "\"Hello World\"" CSV.open('output.txt', 'w') do |csv| csv << [s] end ``` Inside my file, I get: ``` """Hello World""" ``` What's with the extra quotes, and what is the correct way to do this? I don't want all fields wrapped in quotes, just some. Using ruby 1.9.3p194