Remove last two columns and append 5 new columns

May 22, 2013 at 7:02 PM
Edited May 22, 2013 at 7:03 PM
I have used the KBCSV for more simplistic things and I cannot seem to figure out how to resolve this latest issue.

I have a CSV with about 150 rows.

I want to remove a specified number of columns at the end of every row, and then append 5 new columns to each row.

Ultimately I then want to combine this CSV with another (append at the end), which I can already do.

Can anyone offer some assistance with how I would begin going about this? Significant changes have happened to the kbcsv since I last used it.

Either in or c# is useful. Thanks
May 28, 2013 at 12:51 PM
Hi Daniel,

There are various ways of achieving this, but ultimately you just need to pass your modified values to CsvWriter.WriteRecord. Here's one way of doing so:
var csv = "0,1,2,3,4,5,6,7,8,9";

using (var sw = new StringWriter())
    using (var r = CsvReader.FromCsvString(csv))
    using (var w = new CsvWriter(sw))
        while (r.HasMoreRecords)
            var record = r.ReadDataRecord();

            // here's the important bit...

            // get a list of values in the record
            var values = record.ToList();

            // remove the last 5
            values.RemoveRange(values.Count - 5, 5);

            // add some more

            // write the transformed values to the CsvWriter