1

Even though I am inserting value as a string in CSV its getting stored as number e.g. "01" getting stored as 1.

I am using CSV writer:

@out = File.open("#{File.expand_path("CSV")}/#{file_name}.csv", "w")
CSV::Writer.generate(@out) do |csv|
  csv << ["01", "02", "test"]
end
@out.close

This generates csv with given values but when we open csv using excel "01" is not stored as text it gets stored as number

Thanks

vasu
  • 391
  • 3
  • 13
  • Post a sample of the code you are using. It isn't fair to expect us to guess what you are doing. – the Tin Man Mar 22 '11 at 09:26
  • What's the difference between this and your other question http://stackoverflow.com/questions/5831366/quote-all-fields-in-csv-output – Mark Thomas Nov 07 '13 at 15:15

2 Answers2

1

You have to surround the value with double quotations like "..." in order to get it stored as a string.

sawa
  • 165,429
  • 45
  • 277
  • 381
0

Use string formatting:

my_int = 1 p "%02d" % my_int

start here for Ruby 1.9.2

http://www.ruby-doc.org/core/classes/String.html

and you will see that for a full set of instructions, you need to dig into Kernel::sprintf

fengolly
  • 503
  • 3
  • 8