LoginSignup
11
10

More than 5 years have passed since last update.

RubyでTSVファイルを出力してスプレッドシートにコピペ

Last updated at Posted at 2015-02-28

はじめに

何かしらのデータをRubyで取得して整形し、ファイルに出力したとき、それを他の人に見せたいことがあります。

見せるのはGoogleのスプレッドシートが便利です。が、CSV形式だとスプレッドシートにデータをコピペすることができません。

そこで、TSV形式で出力します。TSVならコピペ可能です。

スクリプト

require 'csv'

CSV.open("hoge.tsv", "w", :col_sep => "\t") do |io|
  io.puts(data_headings) # 見出し
  data.each { |row| io.puts(row) } # 実際のデータ
end

"\t"'\t' のようにシングルクォーテーションになってるとタブとして認識されてないので注意。

参考:[Ruby]シングル/ダブルクォーテーションの差異

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10