I’m using ruby’s File to open and read in a text file inside of a rake task. Is there a setting where I can specify that I want the first line of the file skipped?
Here’s my code so far:
desc "Import users."
task :import_users => :environment do
File.open("users.txt", "r", '\r').each do |line|
id, name, age, email = line.strip.split(',')
u = User.new(:id => id, :name => name, :age => age, :email => email)
u.save
end
end
reader = CSV.open("file_location", "r")
reader.shift # this line is used to skipped first header line
reader.each{|row|
first_field = row[0]
second_field = row[1]
I don't know if CSV has the option, but FasterCSV has an option you can pass to inform it that the first line is the header row and it will skip it automatically...