Tab limited csv files are read incorrectly due to comma in header
CSV files are read incorrectly when there is a comma in one of the header columns.
So I have a tab-delimited CSV file in which one of the header columns has a comma in it as shown in the example below
Name Currency Type Cost "Cost per 1,000 Items"
Test1 ABC XX "10,143.27" 0.00
Test2 EFG YY "10,143.27" 0.00
Test3 IJK ZZ "10,143.27" 0.00
The header is read as 2 columns instead of 5 as it breaks on the comma. i.e, Column1 = Name Currency Type Cost "Cost per 1 Column2 = 000 Items"
Due to this even the data rows also break on the comma, for eg: row 1 is read as column1 = Test1 ABC XX "10 column2 = 143.27" 0.00
This only affects the files that have a comma in the header. When the header is fine, the rows are read correctly even if it has values with a comma. Therefore, the comma in the header is causing it.
I'm using the reader with the default configuration.
using (var stream = File.Open(FilePath, FileMode.Open, FileAccess.Read))
{
var reader = ExcelReaderFactory.CreateCsvReader(stream)
using (reader)
{
do
{
while(reader.Read())
{
}
} while(reader.NextResult());
DataTable = reader.AsDataSet(new ExcelDataSetConfiguration()
{
UseColumnDataType = true,
ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
}).Tables[0];
}
}
Can someone help with this?
Hi,
This is probably another one of those cases where the separator autodetector fails, and decides to use the comma instead of the tab character. There are ways to improve the statistics heuristics in the library, but it's not been prioritized to look into.
To progress, you could look into passing AutodetectSeparators = new char[] { '\t' } as an option to CreateCsvReader to force using tab as the separator.
Thanks for the reply. We actually rely on the autodetection heavily. We receive multiple files with different delimiters and therefore enforcing tab as the separator doesn't help us. I will have to handle this some other way.
Is there a plan to fix this at any time in the future?
As an FYI, the autodetector is by definition fuzzy, because the CSV format is rather informally specified. We can improve the heuristics based on data shown to us (like in your case, and also in #360), but we'll never be able to implement a separator autodetector algorithm that works for 100% of CSVs out there.
So if you are dealing with a lot of different kinds of CSV, the proper way to implement this is to 1) preview the import results to the user and 2) let the user change/pick separator in case the wrong separator is autodetected.
To answer your final question, alas I don't think this is planned to be fixed anytime soon.
Got it, thanks!