CsvHelper
CsvHelper copied to clipboard
Raise an exception if a CSV row contains extra values?
Currently, if a CSV contains a row with fewer values than expected (i.e. too few delimiters), an exception will be raised. However, if a CSV contains a row with more values than expected (i.e. too many delimiters), CsvHelper will silently ignore the extra values and parse just the first part of the row.
Is there currently any way to raise an exception in this situation? I have had a look and not been able to find any.
I am working with dynamic records, but I think the same thing would happen with strongly-typed records, so long as the apparent column values were able to be shoehorned into the expected fields.
The behaviour I would expect and prefer is that if a CSV is malformed, with inconsistent dimensions, an exception is always raised. This is better behaviour since an extra delimiter probably indicates that there has been some kind of error either in the data, or in the parsing of quotes/delimiters. I would rather receive an exception than ingest incorrect data.
This would also be more consistent with the behaviour of libraries in other languages. If I read a CSV in a language geared towards data manipulation, such as R or Python (using Pandas), I will get an exception if the CSV is in any way malformed.
As a trivial example, the following CSV with an extra comma
FirstName,Surname,City
,Jane,Simons,Sydney
John,Smith,Melbourne
would currently result in a surname of Jane
and a city of Simons
, with the value Sydney
being silently lost during data ingest.
Would a PR in this area be welcomed?