开发者

fasterCsv exception handling

开发者_如何学编程

I am facing the MalformedCSVError problem in csv file .And the line is given below where I am stuck:

"# literally hundreds of sources of "IP-to-Country" databases available. Man"

Because the line contains the double quote.Execution stopped here Exception is: FasterCSV::MalformedCSVError

How I can handle this situation.I can not edit the csv file as well..

Please help me on this.


That example really is malformed. The quote character appears to be double-quotes but there are embedded double quotes within it (should be escaped with double quotes again). You really should reject this as malformed.

As a bit of a hacky work-around, you can try changing the quote character From the documentation for FasterCSV, you can pass :quote_char => "'" and that should will let you access the data but now you'll be getting the double quotes as part of the string - messy.

Really, you shouldn't need to resort to editing the source data - your program should handle the error gracefully, inform someone and continue.

We use FasterCSV in batch mode behind NGINX. When an error occurs parsing a line, we catch the exception, note the line that caused the error and continue. After all rows are parsed, we email the user to let them know which rows could not be processed.

Something like this works for us ...

    io.each_line do |line|

        the_line = line.chomp
        begin
            row = FasterCSV.parse_line(the_line)
            ok, message = acceptable?(row)

            if not ok
                reject(io.lineno, the_line, message)
            else
                accept(row, the_line)
            end

        rescue FasterCSV::MalformedCSVError => e
            reject(io.lineno, the_line, e.to_s)
        end

    end

Note the handlers to accept and reject which allow us to decouple the acceptance and rejection from the core of parsing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜