Created
March 9, 2012 08:21
-
-
Save ohnishiakira/2005628 to your computer and use it in GitHub Desktop.
標準のCSVライブラリではCSV::MalformedCSVErrorが起きるCSVの処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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