English 中文(简体)
Ignore First line on csv parse Railway
原标题:Ignore first line on csv parse Rails

我正在使用以下代码:,该图人将CSV档案整理成册,并将内容输入数据库表。 我怎么会忽略特别志愿人员档案的第一行? 管制人员守则如下:

def csv_import 
  @parsed_file=CSV::Reader.parse(params[:dump][:file])
  n = 0
  @parsed_file.each  do |row|
    s = Student.new
    s.name = row[0]
    s.cid = row[1]
    s.year_id = find_year_id_from_year_title(row[2])
    if s.save
      n = n+1
      GC.start if n%50==0
    end
    flash.now[:message] = "CSV Import Successful, #{n} new students added to the database."
  end
  redirect_to(students_url)
end
最佳回答
@parsed_file.each_with_index  do |row, i|
  next if i == 0
  ....
问题回答

这个问题在寻找如何跳出第一线与CSV/FengerCSV图书馆时不断发生,因此,如果你在此结束,就可找到解决办法

the solution is... CSV.foreach("path/to/file.csv",{:headers=>:first_row}) do |row|

HTH.

如果你确定你的第一行为头盔,那么你就回到了<代码>。 http://www.un.org。

当贵重电池值时,似乎需要使用<代码>fetch(“Row Owner”)。

这是我提出的。 缩略语

CSV.foreach("GitHubUsersToAdd.csv",{:headers=>:first_row}) do |row| username = row.fetch("GitHub Username") if username puts username.inspect end end

借助这一简单法典,你可以读到CSV档案,忽视头线或外地名称:

CSV.foreach(File.join(File.dirname(__FILE__), filepath), headers: true) do |row|
    puts row.inspect
end

你可以做你想要的<代码>row。 Don tabesheaders: real

require  csv 
csv_content =<<EOF
lesson_id,user_id
5,3
69,95
EOF

parse_1 = CSV.parse csv_content
parse_1.size # => 3  # it treats all lines as equal data

parse_2 = CSV.parse csv_content, headers:true
parse_2.size # => 2  # it ignores the first line as it s header

parse_1
# => [["lesson_id", "user_id"], ["5", "3"], ["69", "95"]]     
parse_2
# => #<CSV::Table mode:col_or_row row_count:3> 

页: 1

parse_1.each do |line|
  puts line.inspect        # the object is array
end
# ["lesson_id", "user_id"]
# ["5", " 3"]
# ["69", " 95"]


parse_2.each do |line|
  puts line.inspect        # the object is `CSV::Row` objects
end
# #<CSV::Row "lesson_id":"5" "user_id":" 3">
# #<CSV::Row "lesson_id":"69" "user_id":" 95">

因此,我可以这样做。

parse_2.each do |line|
  puts "I m processing Lesson #{line[ lesson_id ]} the User #{line[ user_id ]}"
end
# I m processing Lesson 5 the User 3
# I m processing Lesson 69 the User 95
data_rows_only = csv.drop(1)

这样做

csv.drop(1).each do |row|
  # ...
end

选择





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...