15

If I have some LF converted (using N++) CSV files, everytime I write data to them using JoshClose's CsvHelper the line endings are back to CRLF.

Since I'm having problems with CLRF ROWTERMINATORS in SQL Server, I whish to keep my line endings like the initital status of the file.

Couldn't find it in the culture settings, I compile my own version of the library.

How to proceed?

4 Answers 4

18

Missing or incorrect Newline characters when using CsvHelper is a common problem with a simple but poorly documented solution. The other answers to this SO question are correct but are missing one important detail.

Configuration allows you to choose from one of four available alternatives:

// Pick one of these alternatives
CsvWriter.Configuration.NewLine = NewLine.CR;   
CsvWriter.Configuration.NewLine = NewLine.LF;
CsvWriter.Configuration.NewLine = NewLine.CRLF;
CsvWriter.Configuration.NewLine = NewLine.Environment;

However, many people are tripped up by the fact that (by design) CsvWriter does not emit any newline character when you write the header using CsvWriter.WriteHeader() nor when you write a single record using CsvWriter.WriteRecord(). The reason is so that you can write additional header elements or additional record elements, as you might do when your header and row data comes from two or more classes rather than from a single class.

CsvWriter does emit the defined type of newline when you call CsvWriter.NextRecord(), and the author, JoshClose, states that you are supposed to call NextRecord() after you are done with the header and after you are done with each individual row added using WriteRecord. See GitHub Issues List 929

When you are writing multiple records using WriteRecords() CsvWriter automatically emits the defined type of newline at the end of each record.

In my opinion this ought to be much better documented, but there it is.

3
  • 6
    It's 2020 and the CsvHelper's documents still lacks important information and various samples. I faced the same problem that header and content were on a single line if using CsvWriter.WriteRecord(). CsvWriter.NextRecord() solved it! Thanks to you no more brain cells will be dying of it anymore :)
    – J Pollack
    Aug 24, 2020 at 14:53
  • 1
    Adding even more confusion, CsvWriter.Flush() states Serializes the row to the TextWriter., which would seemingly indicate the row is finalized. It would be helpful if this method info hinted that you should call NextRecord() to write the NewLine char. Dec 10, 2020 at 0:06
  • Doesn't read csv written by python csv library as of Aug 23, 2022.
    – jjxtra
    Aug 23, 2022 at 16:36
9

From what I can tell, the line terminator isn't controlled by CvsHelper. I've gotten it to work by adjusting the File writer I pass to CsvWriter.

TextWriter tw = File.CreateText(filepathname);
tw.NewLine = "\n";
CsvWriter csvw = new CsvWriter(tw);
csvw.WriteRecords(records);
csvw.Dispose();
4

Might be useful for somebody:

    public static void AppendToCsv(ShopDataModel shopRecord)
    {
        using (var writer = new StreamWriter(DestinationFile, true))
        {
            using (var csv = new CsvWriter(writer))
            {
                csv.WriteRecord(shopRecord);
                writer.Write("\n");
            }
        }
    }
4

As of CsvHelper 13.0.0, line-endings are now configurable via the NewLine configuration property.

E.g.:

using CsvHelper;
using CsvHelper.Configuration;
using System.Globalization;

void Main()
{
    using (var writer = new StreamWriter(@"my-file.csv"))
    {
        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
        {
            csv.Configuration.HasHeaderRecord = false;
            csv.Configuration.NewLine = NewLine.LF; // <<####################

            var records = new List<Foo>
            {
                new Foo { Id = 1, Name = "one" },
                new Foo { Id = 2, Name = "two" },
            };

            csv.WriteRecords(records);
        }
    }
}

private class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}
2
  • This works between records, but can you get a newline between header and first record?
    – RickyTad
    Feb 12, 2020 at 14:29
  • @RickyTad I'm not sure I understand your question. The sample code above does insert a new line after writing the header: header\nrecord1\nrecord2 etc Feb 12, 2020 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.