skip header row when parsing .csv file
I’m using a StreamReader object to provide source data for the LinqtoCSV library. Something like this…
CsvFileDescription fileDescription = new CsvFileDescription()
{
SeparatorChar = ';',
MaximumNbrExceptions = 20,
};
开发者_运维技巧 CsvContext context = new CsvContext();
System.IO.MemoryStream stream = new System.IO.MemoryStream(file.Data);
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
rows = context.Read<T>(reader, fileDescription);
I’m processing csv files which have an extra semi colon in the header row e.g.
Header1; Header2;Header3;
This triggers errors because the CSVtoLinQ library is expecting 4 header names instead of 3. I can read selected rows into an IEnumerable object, but I can’t directly manipulate the contents of the StreamReader object. I’d prefer to continue with the combination of StreamReader/LinqtoCSV as it provides a lot of validation functionality, but because of the file header issue I need to look at alternatives.
Are there other csv import libraries that provide (i) field based validation when parsing a file (ii) a means of skipping the header row?
You could write a method that opens the file, checks the header/fixes it, then saves to a new file which now ready for the existing process.
精彩评论