I have a question in trying to parse a csv file, the given file is formatted like:
Header1,header2,header3
value1,value2,value3
value4,,value5,value6
In row 3, there is an extra comma after value4, where there should only be one, and I am trying to remove it or replace it with a blank space so the file will look like:
value4,value5,value6
Here is my code so far:
var textwriter = Console.Out;
using (var csvWriter = new CsvWriter(textwriter, CultureInfo.InvariantCulture))
using (var reader = new StringReader(@"csvfile.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.MissingFieldFound = null;
csv.Configuration.IgnoreBlankLines = true;
csv.Configuration.RegisterClassMap<CsvTable_Headers_Map>();
csv.Read();
csv.ReadHeader();
csv.Configuration.ShouldSkipRecord = row => row[0].StartsWith("__") || row[0].StartsWith("T");
//above line is used to ignore records in real files
var records = csv.GetRecords<CsvTable_Headers>();
csvWriter.WriteRecords(records);
}
Would there be a way like in the ShouldSkipRecord line where I could detect if there is an extra comma, then remove it? I also tried using an if statement like:
if(records.Contains("value4,")
{
// I know this logic does not work, trying to see if there is a workaround
replace("value4,") with("value4")
}
So with this, would there be a way to remove the extra comma after value4?