How to ignore spaces when using CsvHelper, CsvReader.Read ()?

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

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

I checked Github and CsvReader.Read() , it seems I used SkipEmptyRecords , but this does not work for me, since I have spaces.

Here is my csv file encoded in UTF8 without specification.

enter image description here

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

Link: http://joshclose.imtqy.com/CsvHelper/

+6
source share
1 answer

I believe that it is best to fix the library yourself.

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

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

However, the IsRecordEmpty () method is not ideally implemented to support your script, as it uses the following code:

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

This does not work because your lines are not null or empty. In my opinion, combining Trimming with SkipEmptyRecords could work theoretically:

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

But again, cropping is not used to check if the field is empty or not, so I’m sure that your only option is to fix the library yourself and use the cropping in the IsRecordEmpty () method or use IsNullOrWhiteSpace instead of IsNullOrEmpty.

+4
source

Source: https://habr.com/ru/post/978619/


All Articles