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
I tried line.lineno
and also doing File.open("users.txt", "r", '\r').each do |line, index|
and next if index == 0
but have not had any luck.
In Python, while reading a CSV using the CSV module you can skip the first line using next() method.
Python continue statement is used to skip the execution of the current iteration of the loop. We can't use continue statement outside the loop, it will throw an error as “SyntaxError: 'continue' outside loop“. We can use continue statement with for loop and while loops.
Change each
to each_with_index do |line, index|
and next if index == 0
will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With