csv_converters.rb 591 B

12345678910111213141516171819202122232425262728
  1. #!/usr/local/bin/ruby -w
  2. # csv_converters.rb
  3. #
  4. # Created by James Edward Gray II on 2006-11-05.
  5. # Copyright 2006 Gray Productions. All rights reserved.
  6. require "faster_csv"
  7. # convert a specific column
  8. options = {
  9. :headers => true,
  10. :header_converters => :symbol,
  11. :converters => [
  12. lambda { |f, info| info.index.zero? ? f.to_i : f },
  13. lambda { |f, info| info.header == :floats ? f.to_f : f }
  14. ]
  15. }
  16. table = FCSV(DATA, options) { |csv| csv.read }
  17. table[:ints] # => [1, 2, 3]
  18. table[:floats] # => [1.0, 2.0, 3.0]
  19. __END__
  20. ints,floats
  21. 1,1.000
  22. 2,2
  23. 3,3.0