ruby on rails - How parse the data from TXT file with tab separator? -
i using ruby 1.8.7 , rails 2.3.8. want parse data txt dump file separated tab.
in txt dump contain css property has invalid data.
when run code using fastercsv gem
fastercsv.foreach(txt_file, :quote_char => '"',:col_sep =>'\t', :row_sep =>:auto, :headers => :first_row) |row| col= row.to_s.split(/\t/) puts col[15] end
the error written in console "illegal quoting on line 38." can 1 suggest me how skip row has invalid data , proceed data load process of remaining rows?
here's 1 way it. go lower level, using shift
parse each row , silent malformedcsverror
exception, continuing next iteration. problem loop doesn't nice. if can improve this, you're welcome edit code.
fastercsv.open(filename, :quote_char => '"', :col_sep => "\t", :headers => true) |csv| row = true while row begin row = csv.shift break unless row # things row here... rescue fastercsv::malformedcsverror next end end end
Comments
Post a Comment