1

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?

0

2 Answers 2

2

It should be false, not :false.

Sign up to request clarification or add additional context in comments.

3 Comments

ok, but also, how to read first 20 entries?>
@brabertaser1992 It is better to make two questions and not ask two questions in one.
@knut i know, but here stackoverflow.com/questions/14114129/ruby-view-csv-data i get answer, which first code is not working for me
0

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.

4 Comments

ok, thank you... but please move it from here to stackoverflow.com/questions/14114129/ruby-view-csv-data
the Tin Mans answer contains already the solution.
csv[0,10].each{|line| was what i need: he didn't write it for each loop
The first solution in the selected answer to that question is array = CSV.read('test.csv')[0, 2]. It's your job to understand you can iterate over an array slice of [0, 2] or [0, 10] using each.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.