红宝石查看CSV数据 [英] Ruby view csv data

查看:248
本文介绍了红宝石查看CSV数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从CSV文件中得到一些数据(也如何在CSV选择前20条),例如:

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

A B C
D E F

还方式:

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

所以它是数组的数组...
但如何才能使我normaly查看它在HAML看法?
如何查看数组的数组?
我想是这样(也是每个文件我已经列数不同):
观点:

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]

请帮我查看CSV数据。

Help me please to view csv data.

推荐答案

有几种方法可以读取记录了一组号码,你需要选择一种基于数据源的预期大小来使用。

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.

用CSV文件启动:

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"
    ]
]

另一种是:

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"
    ]
]

这两个都很好,小的输入文件,但如果你正在阅读从一个大的输入文件的几行会出现问题。他们将迫使Ruby来读取整个文件到内存中,并创建割掉你想要的记录数之前的中间阵列。我在哪里工作没什么让我们获得技嘉的文件大小,所以抓住这些文件的一小部分会使得Ruby和制做工作,建立中间数组,然后把它扔了大量资金。

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.

您都关闭为只读的记录所需的最小数目更好。有时不拘一格读数之前要跳过;这表明理念,以处理一起的EOFError 如果输入文件的EOF被意外地遇到了:

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  

替换 5 用的记录数量跳过,而 2 与阅读次数。对于这个例子我特意读出该文件的末尾,以显示如何跳过线,读了一些,然后清洁处理EOF情况。

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.

中的数据是这样的:

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

由于我使用 File.open 与块,文件将被自动块存在后关闭,避免留下一个开放的文件句柄游逛。

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.

你的问题的HAML输出部分没有很好地在所有定义的,但是这是一种方式输出的数据:

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)

这将导致这个输出一个简单的HTML表格:<​​/ P>

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>


编辑:

和我的测试文件:dl.dropbox.com/u/59666091/qnt_small.csv我希望看到在这个浏览器7列(通过HAML视图)

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

这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>

我做了一个小的改动,以移动阵列之外 File.open 块,没有别的不同。

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

这篇关于红宝石查看CSV数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆