Reputation: 1309
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?
Upvotes: 12
Views: 13943
Reputation: 119
I have used simple way to parse csv data. Here delimiters are tab, space, comma or semicolon. It return array of fields.
row_data = File.new("your_file.csv").read
row_data = row_data.split(/[ ,;\s]/).reject(&:empty?)
Upvotes: 0
Reputation: 33752
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.
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
http://as.rubyonrails.org/classes/HashWithIndifferentAccess.html
http://api.rubyonrails.org/classes/ActiveSupport/HashWithIndifferentAccess.html
Upvotes: 2
Reputation: 84
I have had success with FasterCSV and Ruby 1.8.7, I believe it's now the core csv library in 1.9, using this:
table = FasterCSV.read(result_file.to_file.path, { :headers => true, :col_sep => "\t", :skip_blanks => true })
unless table.empty?
header_arry = Array.new
table.headers.each do |h|
#your header logic, e.g.
# if h.downcase.include? 'pos'
# header_arry << 'position'
# end
# simplest case here
header_arry << h.downcase
#which produces an array of column names called header_arry
end
rows = table.to_a
rows.delete_at(0)
rows.each do |row|
#convert to hash using the column names
hash = Hash[header_arry.zip(row)]
# do something with the row hash
end
end
Upvotes: 4