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 ?
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 :
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.
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.
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]; }
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With