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 Thanks, Andrew -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This is by no means a RoR question. But the solution is pretty simple, so I''ll give in. One way to do it would be to just put "next if line.lineno == 1" at the beginning of the loop. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/AKelUIDMYwYJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I am using csv... it is work: if you are interested try this way: require ''csv'' 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] Module_Name.create( :attribute1 => first_field, :attribute2 => second_field ) } Bye:) bdeveloper01 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2012-Jan-27 16:58 UTC
Re: Skipping the First Line when reading in a text file
On Jan 26, 2012, at 8:18 PM, Bala TS wrote:> I am using csv... > > it is work: > > if you are interested try this way: > > require ''csv'' > > 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] > > Module_Name.create( > :attribute1 => first_field, > :attribute2 => second_field > ) > }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... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hai! That is depending upon gem. if you use csv then follow about steps and if you use fastercsv then follows this link http://fastercsv.rubyforge.org/ Bye:) bdeveloper01 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.