Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSVHelper does not parse my Tab delimited CSV file

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 ?

like image 840
Danielle Avatar asked Mar 01 '20 12:03

Danielle


People also ask

Is there a delimiter in the CSV file?

I always got delimiter is "," but actually in the csv file the delimiter is " ". Since I had to deal with the possibility that, depending on the localization settings of the user, the CSV file (Saved in MS Excel) could contain a different delimiter, I ended up with the following approach :

How to read a CSV file with csvhelper?

The type of data is IEnumerable<Person>. CsvHelper will automatically map each column to the property with the same name. For example, the value in FirstName column will be mapped into Person.FirstName. We can then iterate data and access the values in each row. The CsvConfiguration class has many configurables to control how we read a CSV file.

Is there a default delimiter for CSV files in Norway?

Yeah, in Norway as in the Netherlands, as probably in all countries that use comma as decimal separator, the default delimiter for csv files is a semicolon. Excel, for example, will do that in a norwegian locale. So not handling that seems like the same kind of fairly typical culture blindness as not dealing with non-ascii characters.

Why CSV helper won't find header line?

Csv helper won't find header line, because it has been read in the Reader reader.BaseStream.Position = 0; reader.DiscardBufferedData (); foreach (var possibleDelimiter in possibleDelimiters) { if (headerLine.Contains (possibleDelimiter)) { return possibleDelimiter; } } return possibleDelimiters [0]; }


Video Answer


1 Answers

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);
like image 73
Danielle Avatar answered Sep 18 '22 19:09

Danielle