@out = File.open("#{File.expand_path("CSV")}/#{file_name}.csv", "w")
CSV::Writer.generate(@out) do |csv|
csv << ["01", "02", "test"]
end
@out.close
When i run above code it stores the values in CSV as
01, 02. test
I want them to store as
"01", "02", "test"
To do this, select the column of data that has the extra quote marks, then go to the “Data” tab and click “Text to Columns.” In the “Text to Columns” wizard, select “Delimited” and click “Next.” Then, uncheck the “Tab” option and check the “Other” option.
You can put quotes, dashes and spaces in the CSV file. Fields that contain a special character (comma, newline, or double quote), must be enclosed in double quotes. If a field's value contains a double quote character it is escaped by placing another double quote character next to it.
Change
CSV::Writer.generate(@out)do |csv|
to
CSV::Writer.generate(@out, {:force_quotes=>true}) do |csv|
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With