Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9065881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:36:58+00:00 2026-06-16T16:36:58+00:00

I’m getting from csv file some data (also how to select first 20 in

  • 0

I’m getting from csv file some data (also how to select first 20 in csv?), for example:

A B C
D E F

also method:

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

so it is array of arrays…
But how can i view it normaly in haml view?
How can i view array of arrays?
i tried something like (also in each file i have different count of columns):
view:

= @csv.first.each do |a|
 = a[:1]

Help me please to view csv data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:36:59+00:00Added an answer on June 16, 2026 at 4:36 pm

    There are several ways you can read a set number of records, and you need to pick which to use based on the anticipated size of your data source.

    Starting with a CSV file:

    A1,A2,A3
    B1,B2,B3
    C1,C2,C3
    D1,D2,D3
    E1,E2,E3
    F1,F2,F3
    

    The simplest ways to read a fixed number of records would be one of these:

    require 'csv'
    
    array = CSV.read('test.csv')[0, 2]
    

    Which returns an array of two sub-arrays:

    [
        [0] [
            [0] "A1",
            [1] "A2",
            [2] "A3"
        ],
        [1] [
            [0] "B1",
            [1] "B2",
            [2] "B3"
        ]
    ]
    

    An alternate is:

    File.new('test.csv').readlines[0, 2].map{ |l| CSV.parse(l).flatten }
    

    Which returns the same result, an array of two sub-arrays:

    [
        [0] [
            [0] "A1",
            [1] "A2",
            [2] "A3"
        ],
        [1] [
            [0] "B1",
            [1] "B2",
            [2] "B3"
        ]
    ]
    

    Both of these are fine for small input files, but will have problems if you are reading a few lines from a big input file. They will force Ruby to read the entire file into memory and create an intermediate array before slicing off the number of records you want. Where I work it’s nothing for us to get gigabyte file sizes, so grabbing a small section of those files would make Ruby and the system do an inordinate amount of work building the intermediate array then throwing it away.

    You are better off to only read the minimum number of records needed. Sometimes lines need to be skipped before reading; This demonstrates that idea, along with handling EOFError if the input file’s EOF is encountered unexpectedly:

    File.open('test.csv') do |fi|
      array = []
      begin  
        5.times { fi.readline }    
        2.times.each{ array += CSV.parse(fi.readline) }    
      rescue EOFError    
      end    
    end  
    

    Replace 5 with the number of records to skip, and 2 with the number to read. For that example I deliberately read off the end of the file to show how to skip lines, read some and then handle the EOF situation cleanly.

    The data looks like:

    [
        [0] [
            [0] "F1",
            [1] "F2",
            [2] "F3"
        ]
    ]
    

    Because I’m using File.open with a block, the file is closed automatically after the block exists, avoiding leaving an open filehandle hanging around.

    The HAML output section of your question isn’t well defined at all, but this is one way to output the data:

    array = []
    File.open('test.csv') do |fi|
      begin  
        0.times { fi.readline }    
        2.times.each{ array += CSV.parse(fi.readline) }    
      rescue EOFError    
      end    
    end  
    
    require 'haml'
    
    engine = Haml::Engine.new(<<EOT)
    %html
      %body
        %table
          - array.each do |r|
            %tr
              - r.each do |c|
                %td= c
    EOT
    puts engine.render(Object.new, :array => array)
    

    Which results in this output of a simple HTML table:

    <html>
      <body>
        <table>
          <tr>
            <td>A1</td>
            <td>A2</td>
            <td>A3</td>
          </tr>
          <tr>
            <td>B1</td>
            <td>B2</td>
            <td>B3</td>
          </tr>
        </table>
      </body>
    </html>
    

    EDIT:

    and my test file: dl.dropbox.com/u/59666091/qnt_small.csv i want to see this 7 columns in browser (via haml view)

    Using this as my test data:

    a1,a2,a3,a4,a5,a6,a7
    b1,b2,b3,b4,b5,b6,b7
    c1,c2,c3,c4,c5,c6,c7
    d1,d2,d3,d4,d5,d6,d7
    e1,e2,e3,e4,e5,e6,e7
    

    and this code:

    require 'csv'
    
    array = []
    File.open('test.csv') do |fi|
      begin
        0.times { fi.readline }
        2.times.each{ array += CSV.parse(fi.readline) }
      rescue EOFError
      end
    end
    
    require 'haml'
    
    engine = Haml::Engine.new(<<EOT)
    %html
      %body
        %table
          - array.each do |r|
            %tr
              - r.each do |c|
                %td= c
    EOT
    puts engine.render(Object.new, :array => array)
    

    I get this output:

    <html>
      <body>
        <table>
          <tr>
            <td>a1</td>
            <td>a2</td>
            <td>a3</td>
            <td>a4</td>
            <td>a5</td>
            <td>a6</td>
            <td>a7</td>
          </tr>
          <tr>
            <td>b1</td>
            <td>b2</td>
            <td>b3</td>
            <td>b4</td>
            <td>b5</td>
            <td>b6</td>
            <td>b7</td>
          </tr>
        </table>
      </body>
    </html>
    

    I made a minor change to move array outside the File.open block, nothing else is different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm using an ASP request returning a XML file containing some latin characters. By
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.