0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyでアドホックなCSVからのインポートスクリプトを書く時のひな形

Last updated at Posted at 2017-05-21

CSVのデータをDBに取り込みたく、ActiveRecordを使いたい時。最近よく書くので

require 'csv'

def import_row(row)
  # something to do
  return '必須項目がない' if row['必須な列'].blank? # 要 ActiveSupport

  nil
end

if ARGV[0].nil?
  puts 'input csv filename on arguments'
elsif !File.exist?(ARGV[0])
  puts "#{ARGV[0]} is not found"
else
  CSV.foreach(ARGV[0],
              headers: true,
              encoding: 'BOM|UTF-8',
              header_converters: Proc.new { |h| h.strip },
              skip_blanks: true) do |row|
    result = import_row(row)
    puts result + ',' + row.to_csv if result
  end
end
  • ファイルはコマンドライン引数から受け取る。めんどくさいので。
  • 行の処理はメソッドに分ける。これで、エラーメッセージを出してnextみたいなことをしなくてよくなる。後置ifの強みが活かせるように思う。
  • 例外的な行はメソッド内でチェックし、エラー行として出す。メッセージと元の行を出して有効活用する。$INPUT_LINE_NUMBERで行数を出すのも良い。ファイル出力もできるが、一旦は標準出力のリダイレクトで解決。場合により。
  • headers: trueで、ヘッダが日本語でもrow['列名']でアクセスできる。ただ、BOMが付いているとrow.header[0]にもBOMがついてきてアクセスできないとかトラブルもあり、ファイルを見つつやる必要はある。
  • BOMがついてる場合はencoding: 'BOM|UTF-8'で解決。
  • header_converters: Proc.new { |h| h.strip }でヘッダから空白が取れる、といった形で加工もできる。
0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?