Skip to content

Instantly share code, notes, and snippets.

@ohnishiakira
Created March 9, 2012 08:21
Show Gist options
  • Save ohnishiakira/2005628 to your computer and use it in GitHub Desktop.
Save ohnishiakira/2005628 to your computer and use it in GitHub Desktop.
標準のCSVライブラリではCSV::MalformedCSVErrorが起きるCSVの処理
# coding: utf-8
#
# 1行目がヘッダ、2行目からレコードの始まる典型的なCSVだけど
# 標準のCSVライブラリではCSV::MalformedCSVErrorが起きるCSVの処理
#
# 1. File.openで開き、file.readlines[1..-1]で1行目を飛ばして読み込む
# 2. line.split(/,/)でコンマで区切って配列に変換する
# 3. あとはいつも通りに
#
File.open("malformed.csv", "r") do |file|
file.readlines[1..-1].map{|line|
line.split(/,/)
}.each{|row|
# processing
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment