首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

CSV::Table

Parent:ObjectIncluded modules:Enumerable

CSV :: Table是用于表示CSV文档的二维数据结构。通过表格,您可以按行或列处理数据,处理数据,甚至可以将结果转换回CSV(如果需要)。

如果标题行处理被激活,所有由CSV返回的表格将从这个类中构建。

属性

modeR

目前的索引和迭代访问模式。

tableR

用于比较相等的内部数据格式。

公共类方法

new(array_of_rows) Show source

构建一个新的CSV ::表array_of_rows,它们预计为CSV :: Row对象。假定所有行具有相同的标题。

CSV :: Table对象通过委派支持以下Array方法:

  • empty?()
  • length()
  • size()
代码语言:javascript
复制
# File lib/csv.rb, line 586
def initialize(array_of_rows)
  @table = array_of_rows
  @mode  = :col_or_row
end

公共实例方法

<<(row_or_array)显示源文件

将新行添加到此表的底端。您可以提供一个数组,它将被转换为一个CSV :: Row(继承表头())或一个CSV :: Row。

此方法返回用于链接的表。

代码语言:javascript
复制
# File lib/csv.rb, line 784
def <<(row_or_array)
  if row_or_array.is_a? Array  # append Array
    @table << Row.new(headers, row_or_array)
  else                         # append Row
    @table << row_or_array
  end

  self  # for chaining
end

==(other) Show source

如果此表==()other的行的所有行都返回true

代码语言:javascript
复制
# File lib/csv.rb, line 871
def ==(other)
  @table == other.table
end

显示来源

在默认混合模式下,此方法返回索引访问的行和列标题访问的列。您可以先调用#by_col!()或#by_row!()来强制索引关联。

列作为值的数组返回。改变该数组对表格没有影响。

代码语言:javascript
复制
# File lib/csv.rb, line 700
def [](index_or_header)
  if @mode == :row or  # by index
     (@mode == :col_or_row and (index_or_header.is_a?(Integer) or index_or_header.is_a?(Range)))
    @table[index_or_header]
  else                 # by header
    @table.map { |row| row[index_or_header] }
  end
end

[]=(index_or_header, value) 显示源文件

在默认混合模式下,此方法为索引访问分配行并为报头访问分配列。您可以先调用#by_col!()或#by_row!()来强制索引关联。

行可以设置为值的数组(它将继承表头())或CSV :: Row。

列可以被设置为一个值,该值被复制到列的每一行或一组值。数组数组按行的主要顺序从上到下分配给行。超出的值将被忽略,如果数组没有每行的值,多余的行将会收到一个值nil

分配给现有的列或行会破坏数据。分配给新列创建它们在表的右端。

代码语言:javascript
复制
# File lib/csv.rb, line 725
def []=(index_or_header, value)
  if @mode == :row or  # by index
     (@mode == :col_or_row and index_or_header.is_a? Integer)
    if value.is_a? Array
      @table[index_or_header] = Row.new(headers, value)
    else
      @table[index_or_header] = value
    end
  else                 # set column
    if value.is_a? Array  # multiple values
      @table.each_with_index do |row, i|
        if row.header_row?
          row[index_or_header] = index_or_header
        else
          row[index_or_header] = value[i]
        end
      end
    else                  # repeated value
      @table.each do |row|
        if row.header_row?
          row[index_or_header] = index_or_header
        else
          row[index_or_header] = value
        end
      end
    end
  end
end

by_col() 显示源文件

以列模式返回重复的表格对象。这对于在不改变表模式的情况下在单个调用中进行链接很方便,但请注意,此方法会为更大的数据集消耗相当数量的内存。

此方法返回用于链接的重复表。不要以这种方式链接破坏性方法(如[] =()),因为您正在处理重复。

代码语言:javascript
复制
# File lib/csv.rb, line 612
def by_col
  self.class.new(@table.dup).by_col!
end

by_col!() 显示源文件

将此表的模式切换到列模式。对索引和迭代方法的所有调用都将与列一起工作,直到模式再次更改。

此方法返回表并且可以安全链接。

代码语言:javascript
复制
# File lib/csv.rb, line 622
def by_col!
  @mode = :col

  self
end

by_col_or_row() 显示源文件

以混合模式返回重复的表格对象。这对于在不改变表模式的情况下在单个调用中进行链接很方便,但请注意,此方法会为更大的数据集消耗相当数量的内存。

此方法返回用于链接的重复表。不要以这种方式链接破坏性方法(如[] =()),因为您正在处理重复。

代码语言:javascript
复制
# File lib/csv.rb, line 637
def by_col_or_row
  self.class.new(@table.dup).by_col_or_row!
end

by_col_or_row!() 显示源文件

将此表的模式切换到混合模式。所有对索引和迭代方法的调用都将使用默认的智能索引系统,直到模式再次更改。在混合模式下,索引被假定为行引用,而其他任何东西都被假设为按列标题访问。

此方法返回表并且可以安全链接。

代码语言:javascript
复制
# File lib/csv.rb, line 649
def by_col_or_row!
  @mode = :col_or_row

  self
end

by_row() 显示源文件

以行模式返回重复的表格对象。这对于在不改变表模式的情况下在单个调用中进行链接很方便,但请注意,此方法会为更大的数据集消耗相当数量的内存。

此方法返回用于链接的重复表。不要以这种方式链接破坏性方法(如[] =()),因为您正在处理重复。

代码语言:javascript
复制
# File lib/csv.rb, line 664
def by_row
  self.class.new(@table.dup).by_row!
end

by_row!()显示源文件

将此表的模式切换到行模式。对索引和迭代方法的所有调用都将与行一起使用,直到模式再次更改。

此方法返回表并且可以安全链接。

代码语言:javascript
复制
# File lib/csv.rb, line 674
def by_row!
  @mode = :row

  self
end

delete(index_or_header) 显示源文件

删除并返回指定的列或行。在默认混合模式索引中引用行,其他所有内容都假定为列标题。使用#by_col!()或#by_row!()强制查找。

代码语言:javascript
复制
# File lib/csv.rb, line 812
def delete(index_or_header)
  if @mode == :row or  # by index
     (@mode == :col_or_row and index_or_header.is_a? Integer)
    @table.delete_at(index_or_header)
  else                 # by header
    @table.map { |row| row.delete(index_or_header).last }
  end
end

delete_if(&block) 显示源文件

删除块返回的任何列或行true。在默认混合模式或行模式下,迭代是行的标准行主要步行。在列模式下,迭代将yield包含两个元素元组,其中包含列名和该列的值数组。

此方法返回用于链接的表。

如果没有给出块,则返回枚举器。

代码语言:javascript
复制
# File lib/csv.rb, line 831
def delete_if(&block)
  block or return enum_for(__method__) { @mode == :row or @mode == :col_or_row ? size : headers.size }

  if @mode == :row or @mode == :col_or_row  # by index
    @table.delete_if(&block)
  else                                      # by header
    to_delete = Array.new
    headers.each_with_index do |header, i|
      to_delete << header if block[[header, self[header]]]
    end
    to_delete.map { |header| delete(header) }
  end

  self  # for chaining
end

each(&block) 显示源文件

在默认混合模式或行模式下,迭代是行的标准行主要步行。在列模式下,迭代将yield包含两个元素元组,其中包含列名和该列的值数组。

此方法返回用于链接的表。

如果没有给出块,则返回枚举器。

代码语言:javascript
复制
# File lib/csv.rb, line 858
def each(&block)
  block or return enum_for(__method__) { @mode == :col ? headers.size : size }

  if @mode == :col
    headers.each { |header| block[[header, self[header]]] }
  else
    @table.each(&block)
  end

  self  # for chaining
end

headers() 显示源文件

返回此表第一行的标题(假定与所有其他行匹配)。空表返回空数组。

代码语言:javascript
复制
# File lib/csv.rb, line 684
def headers
  if @table.empty?
    Array.new
  else
    @table.first.headers
  end
end

inspect() 显示源文件

以US-ASCII字符串显示此表的模式和大小。

代码语言:javascript
复制
# File lib/csv.rb, line 909
def inspect
  "#<#{self.class} mode:#{@mode} row_count:#{to_a.size}>".encode("US-ASCII")
end

push(*rows) 显示源文件

追加多行的快捷方式。相当于:

代码语言:javascript
复制
rows.each { |row| self << row }

此方法返回用于链接的表。

代码语言:javascript
复制
# File lib/csv.rb, line 801
def push(*rows)
  rows.each { |row| self << row }

  self  # for chaining
end

to_a() 显示源文件

以阵列数组的形式返回表。头将是第一行,然后所有的字段行都会跟随。

代码语言:javascript
复制
# File lib/csv.rb, line 879
def to_a
  @table.inject([headers]) do |array, row|
    if row.header_row?
      array
    else
      array + [row.fields]
    end
  end
end

to_csv(options = Hash.new) 显示源文件

以完整的CSV字符串形式返回表格。首先列出标题,然后列出所有字段行。

这种方法假定你想要#headers,除非你明确通过:write_headers => false

代码语言:javascript
复制
# File lib/csv.rb, line 896
def to_csv(options = Hash.new)
  wh = options.fetch(:write_headers, true)
  @table.inject(wh ? [headers.to_csv(options)] : [ ]) do |rows, row|
    if row.header_row?
      rows
    else
      rows + [row.fields.to_csv(options)]
    end
  end.join('')
end

另外别名为:to_s

to_s(options = Hash.new)

别名为:to_csv

values_at(*indices_or_headers) 显示源文件

混合模式默认是将索引列表视为行访问,返回指示的行。其他任何被视为列访问。对于列式访问,返回集每个行都有一个数组,每个数组中的标题指示值。您可以使用#by_col!()或#by_row!()强制列或行模式。

您不能混合列和行访问。

代码语言:javascript
复制
# File lib/csv.rb, line 763
def values_at(*indices_or_headers)
  if @mode == :row or  # by indices
     ( @mode == :col_or_row and indices_or_headers.all? do |index|
                                  index.is_a?(Integer)         or
                                  ( index.is_a?(Range)         and
                                    index.first.is_a?(Integer) and
                                    index.last.is_a?(Integer) )
                                end )
    @table.values_at(*indices_or_headers)
  else                 # by headers
    @table.map { |row| row.values_at(*indices_or_headers) }
  end
end

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com