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

2
  • Post a sample of the code you are using. It isn't fair to expect us to guess what you are doing. Commented Mar 22, 2011 at 9:26
  • What's the difference between this and your other question stackoverflow.com/questions/5831366/… Commented Nov 7, 2013 at 15:15

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.