Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is going to be really difficult. Some things that make FasterCSV, well, <em>faster</em>, make this particularly hard. Here's my best suggestion: FasterCSV can wrap an <a href="http://www.ruby-doc.org/core/IO.html" rel="nofollow">IO</a> object. What you <em>could</em> do, then, is to make your own subclass of <code>File</code> (itself a subclass of <code>IO</code>) that "holds onto" the result of the last <a href="http://www.ruby-doc.org/core/IO.html#method-i-gets" rel="nofollow"><code>gets</code></a>. Then when FasterCSV raises an exception you can ask your special <code>File</code> object for the last line. Something like this:</p> <pre><code>class MyFile &lt; File attr_accessor :last_gets @last_gets = '' def gets(*args) line = super @last_gets &lt;&lt; $/ &lt;&lt; line line end end # then... file = MyFile.open(filename, 'r') csv = FasterCSV.new file row = true while row begin break unless row = csv.shift # do things with the good row here... rescue FasterCSV::MalformedCSVError =&gt; e bad_row = file.last_gets # do something with bad_row here... next ensure file.last_gets = '' # nuke the @last_gets "buffer" end end </code></pre> <p>Kinda neat, right? <em>BUT!</em> there are caveats, of course:</p> <ol> <li><p>I'm not sure how much of a performance hit you take when you add an extra step to every <code>gets</code> call. It might be an issue if you need to parse multi-million-line files in a timely fashion.</p></li> <li><p>This <del><strong>fails utterly</strong></del> might or might not fail if your CSV file contains newline characters inside quoted fields. The reason for this is <a href="https://github.com/circle/fastercsv/blob/master/lib/faster_csv.rb#L1565-1570" rel="nofollow">described in the source</a>--basically, if a quoted value contains a newline then <code>shift</code> has to do additional <code>gets</code> calls to get the entire line. There could be a clever way around this limitation but it's not coming to me right now. If you're sure your file doesn't have any newline characters within quoted fields then this shouldn't be a worry for you, though.</p></li> </ol> <p>Your <em>other</em> option would be to read the file using <code>File.gets</code> and pass each line in turn to <a href="http://fastercsv.rubyforge.org/classes/FasterCSV.html#M000017" rel="nofollow"><code>FasterCSV#parse_line</code></a> but I'm pretty sure in so doing you'd squander any performance advantage gained from using FasterCSV.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload