0

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?

1 Answer 1

2

You could try opening the file differently, something like this:

string fileContent = File.ReadAllText(@"csvfile.csv");
fileContent = fileContent.Replace(",,", ",");
var reader = new StringReader(fileContent);

and then continue with your implementation

Sign up to request clarification or add additional context in comments.

2 Comments

Oh I tried that, but records isnt a string, it is of type Ienumerable
Updated my answer, with a different type of solution, you could try opening the file different, just to get the content as simple string, then you replace the double commas and then move to the same sting reader you're using

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.