Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab delimited file parsing in Rails

I have a file that looks like this

ID Name  Car
1  Mike  Honda
2  Adam  Jim

These values are tab delimited, and from this I want to parse it in Ruby and put it into my database.

I have tried the following

require 'csv'

CSV.foreach("public/files/example.tab", {:col_sep => "\t"}) do |row|
  @stuff = row[0]
end

but @stuff just returns the whole entire object, and doesn't seem to be using the column separator I specified.

It also does not take into account that the first row is a header.

How can I parse a tab delimited file in Ruby and how do I tell it that the first row is a header?

like image 781
Mike Silvis Avatar asked Nov 17 '11 21:11

Mike Silvis


1 Answers

Update

Check out the Gem "smarter_csv" https://github.com/tilo/smarter_csv/ ; it has a couple of interesting features to create hashes from CSV data.

Previous Answer

here's how I'd do it (along the way I convert the "arrays of arrays" which are returned by CSV.read or CSV.parse, into "arrays of hashes"... this makes the data look more like ActiveRecord data, and it's a bit easier to process this way later on..

require 'csv'

def process(csv_array)  # makes arrays of hashes out of CSV's arrays of arrays
  result = []
  return result if csv_array.nil? || csv_array.empty?
  headerA = csv_array.shift             # remove first array with headers from array returned by CSV
  headerA.map!{|x| x.downcase.to_sym }  # make symbols out of the CSV headers
  csv_array.each do |row|               #    convert each data row into a hash, given the CSV headers
    result << Hash[ headerA.zip(row) ]  #    you could use HashWithIndifferentAccess here instead of Hash
  end
  return result
end

# reading in the CSV data is now just one line:

csv_data = process( CSV.read( filename , { :col_sep => "\t"}) )

 => [{:id=>"1", :name=>"Mike", :car=>"Honda"}, 
     {:id=>"2", :name=>"Adam", :car=>"Jim"}] 

you can now process the data like this:

csv_data.each do |hash|
  # ...
end

See also:

http://as.rubyonrails.org/classes/HashWithIndifferentAccess.html

http://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html

like image 188
Tilo Avatar answered Sep 21 '22 14:09

Tilo