6

I am trying to read a tab delimited CSV file and parse it with CSVHelper.

I have the following :

 _reader = new StreamReader(_stream);
 _csvReader = new CsvReader(_reader);
 _csvReader.Configuration.Delimiter = "\t";

but the reader fails to recognize and parse the file correctly

Any ideas ?
What are the possible delimiters with CSVHelper ?

Danielle
  • 3,324
  • 2
  • 18
  • 31

5 Answers5

14

So for some reason, it turns out that it works only when I do the following:

 _reader = new StreamReader(_stream);

 CsvHelper.Configuration.Configuration myConfig = new 
     CsvHelper.Configuration.Configuration();

 _csvReader.Configuration.Delimiter = "\t";

 _csvReader = new CsvReader(_reader, myConfig);
Danielle
  • 3,324
  • 2
  • 18
  • 31
1

Digging though the source code there is a Delimiter parameter. The code below worked for me.

var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = "~" };
using var csv = new CsvReader(reader, config);

source: https://github.com/JoshClose/CsvHelper/blob/master/src/CsvHelper/Configuration/CsvConfiguration.cs

Westly White
  • 543
  • 9
  • 14
0

I encountered a similar problem parsing a tab delimited file in vb, didn't work for me until I replaced "\t" with vbTab

My code:

Imports System.Globalization
Imports System.IO
Imports CsvHelper
Imports CsvHelper.Configuration
Imports CsvHelper.Configuration.Attributes

Public Class TabDelimetedReader
    Public Shared Function ReadFile(path As String) As IEnumerable(Of TabFileDefinition)

        Dim config = New CsvConfiguration(CultureInfo.InvariantCulture,
            hasHeaderRecord:=False,
            delimiter:=vbTab)

        Using reader As StreamReader = New StreamReader(path)
            Using tsv = New CsvReader(reader, config)
                Return tsv.GetRecords(Of TabFileDefinition).ToList
            End Using
        End Using

    End Function
End Class
CY.
  • 11
  • 1
0

I ran into similar issue with a tab delimited file- and I could not get Delimiter = "\t" to work...

Then I added Encoding = Encoding.UTF8 in the configuration, then it worked!

Rishabh Kumar
  • 2,342
  • 3
  • 13
  • 23
GBCode
  • 1
0

Update on the original answer.

(posting as a new answer as the original answer is not editable for some reason)

The original answer was of course correct. Later, the CsvHelper API has slightly changed. Here is a code for current CsvHelper version (30.0.1):


var _reader = new StreamReader(_stream);

CsvHelper.Configuration.CsvConfiguration myConfig = new
    CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
    {
        Delimiter = "\t"
    };

var _csvReader = new CsvReader(_reader, myConfig);
rk72
  • 976
  • 1
  • 10
  • 15