JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.65k stars 1.05k forks source link

Multiple empty lines can make delimiter autodetection fail #2170

Closed ManelBH closed 6 months ago

ManelBH commented 11 months ago

Describe the bug Delimiter autodetection fails in short csv that have multiple empty lines in the end

To Reproduce

var lines = new List<string>() {
    "A;B;C;D",
    "1;2;3;4",
    "1;2;3;4",
    "1;2;3;4",
    "",
    "",
    "",
};
var text = string.Join("\r\n", lines);
using var csv = new MemoryStream(Encoding.UTF8.GetBytes(text));
using var reader = new StreamReader(csv);
using var parser = new CsvParser(reader, new CsvConfiguration(CultureInfo.InvariantCulture) { DetectDelimiter = true});
parser.Read();
Console.WriteLine(parser.Delimiter); // writes ","

Expected behavior In the example the parser should detect ";". These empty lines should be ignored.

i2van commented 11 months ago

Try this:

using var parser = new CsvParser(reader, new CsvConfiguration(CultureInfo.InvariantCulture) {
    DetectDelimiter = true,
    IgnoreBlankLines = true // Does it help?
});
JoshClose commented 6 months ago

Fixed.