Search code examples
c#csvhelper

How to skip a footnote at the end of csv file using CsvHelper


I am trying to read a CSV file in the form of:

Date Value
12/1/2020 1
12/2/2020 2

At the end of the file is a footnote which is all text.

If I use:

void Main()
{
    using (var reader = new StreamReader("path\\to\\file.csv"))
    using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
    {
        var records = csv.GetRecords<Foo>();
    }
}

public class Foo
{
    public DateTime Date { get; set; }
    public double Value { get; set; }
}

CsvHelper throws, "FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."

It will work if I use:

while (csv.Read()) {
   try {
        records.Add(csv.GetRecord<Foo>());
    }
    catch (Exception e) {}
}

but that is really slow.

I was hoping that adding:

csv.Configuration.ShouldSkipRecord = record => record.Contains("The");

would solve the problem, but I get the same error.

Any suggestions would be greatly appreciated. (My apologies for not being able to format the table correctly.)


Solution

  • If the footnote is only the text The, then what you have would work. To do what you're trying to do, you'd need to do something like this:

    csv.Configuration.ShouldSkipRecord = record => record.FirstOrDefault()?.StartsWith("The") ?? false;
    

    You could test if there is only 1 (or less) fields too.

    csv.Configuration.ShouldSkipRecord = record => record.Count() <= 1;