Project

General

Profile

Actions

Feature #10316

closed

Failsafe CSV.foreach

Feature #10316: Failsafe CSV.foreach

Added by tramuntanal (Oliver Valls) about 11 years ago. Updated over 7 years ago.

Status:
Rejected
Target version:
-
[ruby-core:65369]

Description

I've been looking for a CSV.foreach equivalent which either

  • ignores malformed CSV rows
  • or returns a nil row or a row with a parsed success flag.

Can I collaborate to develop this functionallity? Symply sending a patch here?

Updated by hsbt (Hiroshi SHIBATA) about 8 years ago Actions #1 [ruby-core:82434]

  • Assignee deleted (nobu (Nobuyoshi Nakada))

Updated by naruse (Yui NARUSE) almost 8 years ago Actions #2

  • Target version deleted (2.2.0)

Updated by hsbt (Hiroshi SHIBATA) over 7 years ago Actions #3 [ruby-core:85816]

  • Status changed from Open to Assigned
  • Assignee set to kou (Kouhei Sutou)

Updated by kou (Kouhei Sutou) over 7 years ago Actions #4 [ruby-core:85838]

  • Status changed from Assigned to Rejected

You can implement it with the following code:

CSV.open(path) do |csv|
  loop do
    begin
      row = csv.shift
    rescue ArgumentError # For old csv library
      # ignore
    rescue CSV::MalformedCSVError # For the latest csv library (not released yet)
      # ignore
    else
      # Do what you want with "row"
    end
  end
end

See also: https://github.com/ruby/csv/issues/25

Actions

Also available in: PDF Atom