How do you ignore Whitespace when using CsvHelper, CsvReader.Read()?

7.9k views Asked by At

When using the CsvHelper library and in particular the CsvReader.Read() function, is there a way to ignore blank records and/or whitespace?

I need to return the raw string[] but was hoping, I could do some cleanup functions when parsing with the library.

I've checked Github and CsvReader.Read() seems to use SkipEmptyRecords but this doesn't work for me as I have whitespace.

Here's my csv file, its encoded in UTF8 without BOM.

enter image description here

I've tried ASCII encoding, just in case I missed something but that didn't work either.
If no one knows I'll chat to Josh and submit a git request with a fix.

Reference: http://joshclose.github.io/CsvHelper/

1

There are 1 answers

2
Faris Zacina On BEST ANSWER

I think your best bet is to fix the library yourself.

The problem is that the Read method uses the SkipEmptyRecords setting and the IsRecordEmpty method to determine if it should skip a field or not:

while (this.configuration.SkipEmptyRecords && this.IsRecordEmpty(false));

However the IsRecordEmpty() method is not perfectly implemented to support your scenario, because it uses the following code:

Enumerable.All<string>((IEnumerable<string>) this.currentRecord, new Func<string, bool>(string.IsNullOrEmpty));

That does not work because your strings are not null or empty. In my opinion combining Trimming with SkipEmptyRecords could work in theory:

 csv.Configuration.TrimFields = true;
 csv.Configuration.SkipEmptyRecords = true;

But again the trimming is not utilized when checking if the field is empty or not, so i am pretty sure your only option is to fix the library yourself and to use the trimming in the IsRecordEmpty() method OR use IsNullOrWhiteSpace instead of IsNullOrEmpty.