Forums | Admin

Discussion Forums: open-discussion

Start New Thread Start New Thread

 

By: Denis Ahearn
How to get physical line number in CSV [ reply ]  
2010-06-11 20:18
I am using the open and each methods in FasterCSV to parse and process a CSV file. When my code detects an error in the file (either from malformed CSV or from data that fails some business validation logic), I display that error to the user, along with the line/row number in the CSV file where the error occurred.

The problem I am having is that is seems the :lineno attribute stores a logical line number, and not a physical one. When my CSV file contains "entries" that are each contained on a single line, then everything's fine, the logical and physical line numbers are the same. However, when an "entry" spans multiple lines in the file due to line breaks in one of the fields, then the logical line numbers don't match the physical ones. The result is that I end up reporting a line number to the user that doesn't map exactly to the physical line/row in the file that contains the error.

Is there any way using FasterCSV to get the physical line/row number as the file is being parsed? If not, would it be possible to enhance FasterCSV to track both the logical line number (i.e. the current :lineno attribute), as well as the physical line number?