I'm currently using the smarter_csv gem to import data from CSVs to my Rails application. Ideally, I'd like to access the columns imported by number instead of the header name, as the header names could be somewhat inconsistent on the data coming in.
Has anyone had success doing this? Something like the code below would be perfect.
SmarterCSV.process(file,
col_sep: ',',
force_simple_split: false,
downcase_header: false,
row_sep: :auto) do |row|
row[1] #get data from whatever column number here
end
The row is returned as a hash, but I don't think I can count on the order of the hash being consistent. Thanks in advance!
I haven't tested this but maybe try the Custom Header Transformations via Procs
change_headers_to_position = Proc.new {|headers|
headers.each_with_index.map{|h, index| index + 1 }
}
options = {
header_transformations: [:none, change_headers_to_position ]
}
data = SmarterCSV.process('/tmp/test.csv', options)