0

I'm using ruby CSV module to read in a csv file.

One of the values inside the csv file is in format is XXX_XXXXX where X are number. I treat this value as string, actually, but CSV module is reading in these values as XXXXXXXX, as numbers, which I do not want.

Options I am currently using

f = CSV.read('file.csv', {:headers => true, :header_converters => :symbol, :converters => :all} )

Is there a way to tell CSV to not do that?

2 Answers 2

2

f = CSV.read('file.csv', {:headers => true, :header_converters => :symbol)}

Leave out the :converters => :all; that one tries (amongst others) to convert all numerical looking strings to numbers.

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

Comments

1

The :convertors => all causes this, try the following

require "csv"

CSV.parse(DATA, :col_sep => ",", :headers => true, :converters => :all).each do |row|
  puts row["numfield"]
end
__END__
textfield,datetimefield,numfield
foo,2008-07-01 17:50:55.004688,123_45678
bar,2008-07-02 17:50:55.004688,234_56789

# gives
# 12345678
# 23456789

and

CSV.parse(DATA, :col_sep => ",", :headers => true).each do |row|
  puts row["numfield"]
end

__END__
textfield,datetimefield,numfield
foo,2008-07-01 17:50:55.004688,123_45678
bar,2008-07-02 17:50:55.004688,234_56789

# gives
# 123_45678
# 234_56789

2 Comments

Thank you! You are right. Unfortunately steenslag answered before with the same answer... so I chose his/her answer. But thank you!!
yeah, i spended more time documenting my answer, good for the people who hit this in the future, happy coding..

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.