Dav*_*low 26
如果你使用ruby 1.8.X和FasterCSV,它有一个' headers '选项:
csv = FasterCSV.parse(your_csv_file, {:headers => true}) #or false if you do want to read them
Run Code Online (Sandbox Code Playgroud)
如果您使用的是ruby 1.9.X,则默认库基本上是FasterCSV,因此您可以执行以下操作:
csv = CSV.parse(your_csv_file, {headers: true})
Run Code Online (Sandbox Code Playgroud)
csv = CSV.read("file")
csv.shift # <-- kick out the first line
csv # <-- the results that you want
Run Code Online (Sandbox Code Playgroud)
我已经找到了上述问题的解决方案。这是我在 ruby 1.9.X 中完成的方法。
csv_contents = CSV.parse(File.read(file))
csv_contents.slice!(0)
csv=""
csv_contents.each do |content|
csv<<CSV.generate_line(content)
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12045 次 |
| 最近记录: |