Working With CSV Files In Ruby 1.8.7 Without FasterCSV
The following Ruby code sample shows how to bring in data from a csv file with headers in a way that allows you to manipulate the date by header name.
inputfile = ARGV[0] csv = CSV.open(inputfile, 'r') Column = Struct.new(*(csv.shift.map { |f| f.to_sym })) columns = csv.inject([]) do |columns, row| columns << Column[*row] end csv.close columns.each do |c| column1 = c.column1header column2 = c.column2header column3 = c.column3header column4 = c.column4header <put your code that uses the column names to do whatever it is you need to do here> end
Found this solution in the book "Enterprise Integration with Ruby by Maik Schmidt"
Recent Comments