Ruby: CSV :: How to use Table Note

CSV library is difficult to use. At least I'm hard to use. Write it down as a memorandum. I'm glad if you can use it as a reference.

new

Pass a CSV :: Row instance. I'm not sure if there is any other way. I feel it's not intuitive

row1 = CSV::Row.new(["header1", "header2"], ["row1_1", "row1_2"])
table = CSV::Table.new([row1])
pp table
#<CSV::Table mode:col_or_row row_count:2>

Get headers

row1 = CSV::Row.new(["header1", "header2"], ["row1_1", "row1_2"])
table = CSV::Table.new([row1])
pp table.headers
["header1", "header2"]

Get by specifying row

I thought that [0] could get the header, but I got the first row (Is it called row?) Excluding the header. Subscript? It seems that if you specify, you can get a row instead of a header.

row1 = CSV::Row.new(["header1", "header2"], ["row1_1", "row1_2"])
table = CSV::Table.new([row1])

pp table[0]
pp table[1]
pp table["header1"]
#<CSV::Row "header1":"row1_1" "header2":"row1_2">
nil

Get by specifying header

This was an intuitive behavior.

row1 = CSV::Row.new(["header1", "header2"], ["row1_1", "row1_2"])
table = CSV::Table.new([row1])

pp table["header1"]
["row1_1"]

CSV file generation

    headers = ["header1", "header2"]

    row1 = CSV::Row.new(headers, ["row1_1", "row1_2"])
    row2 = CSV::Row.new(headers, ["row2_1", "row2_2"])
    table = CSV::Table.new([row1, row2])

    CSV.open("test.csv", "w", headers: headers, write_headers: true, force_quotes: true) do |csv|
      table.each { |row| csv << row }
    end
$ cat test.csv 
"header1","header2"
"row1_1","row1_2"
"row2_1","row2_2"
Make it a method
  def save_csv_table(csv_table)
    headers = csv_table.headers
    CSV.open("test.csv", "w", { headers: headers, write_headers: true, force_quotes: true }) do |csv|
      csv_table.each{ |row| csv << row }
    end
  end

Recommended Posts

Ruby: CSV :: How to use Table Note
How to use Ruby return
Ruby: How to use cookies
How to use Ruby on Rails
How to use Ruby inject method
[Ruby on Rails] How to use CarrierWave
Ruby length, size, count How to use
[Ruby] How to use slice for beginners
How to use Java Scanner class (Note)
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
How to use Twitter4J
How to use active_hash! !!
How to use hidden_field_tag
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Ruby] How to convert CSV file to Yaml (Yml)
[Ruby] How to use gsub method and sub method
How to use Segmented Control and points to note
[Ruby basics] How to use the slice method
[Java] How to use Map
[Ruby] How to use standard output in conditional branching
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
[Rails] How to use enum
[Ruby] How to use rbenv (version `x.x.x'is not installed)
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
[Ruby] How to comment out
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join
How to use Java Map
Rbenv command to use Ruby
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI