Question

Possible Duplicate:
Ruby view csv data

In my app i'm reading csv file, but why in view i have only second,...,n records, without first line? Here is code:

def common_uploader
    require 'csv' 
    @csv = CSV.read("/#{Rails.public_path}/uploads_prices/"+params[:file], {:encoding => "CP1251:UTF-8", :col_sep => ";", :row_sep => :auto, :headers => :false})
  end

:headers => :false i write... but why i didn't get first line from csv file? (ruby 1.9.3)

So, how to get also first line?

Was it helpful?

Solution

It should be false, not :false.

OTHER TIPS

You can use the [0,20]-method also on csv:

require 'csv' 
csv = CSV.parse(DATA.read, {
  :col_sep => ",", 
  :headers => false
  }
)
csv[0,10].each{|line|
  p line #-> first 10 lines
}
__END__
00,a,b,c
01,a,b,c
02,a,b,c
03,a,b,c
04,a,b,c
05,a,b,c
06,a,b,c
07,a,b,c
08,a,b,c
09,a,b,c
10,a,b,c
11,a,b,c
12,a,b,c
13,a,b,c
14,a,b,c
15,a,b,c
16,a,b,c 
17,a,b,c
18,a,b,c
19,a,b,c
20,a,b,c

But this reads all lines to csv - it is only a restricted output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top