Mylan
Mylan

Reputation: 115

Removing columns in CSV file using c#

I want to remove a column with a specific value. The code below is what I used to remove a row. Can I reverse this to remove a column?

int row = comboBox1.SelectedIndex;
string verw = Convert.ToString(txtChange.Text);

List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(filepath)))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        if (line.Contains(","))
        {
            string[] split = line.Split(',');

            if (split[row] == kill) 
            {
                //achter split vul je de rij in 
            }
            else
            {
                line = string.Join(",", split);
                lines.Add(line);
            }
        }
    }
}
using (StreamWriter writer = new StreamWriter(path, false))
{
    foreach (string line in lines)
        writer.WriteLine(line);
}

Upvotes: 1

Views: 7823

Answers (2)

codein
codein

Reputation: 317

To remove the column by name, here is a little modification to your code example.

        List<string> lines = new List<string>();
        using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(path)))
        {
            string target = "";//the name of the column to skip

            int? targetPosition = null; //this will be the position of the column to remove if it is available in the csv file
            string line;

            List<string> collected = new List<string>();
            while ((line = reader.ReadLine()) != null)
            {
               
                    string[] split = line.Split(',');
                    collected.Clear();
                    
                    //to get the position of the column to skip
                    for (int i = 0; i < split.Length; i++)
                    {
                        if (string.Equals(split[i], target, StringComparison.OrdinalIgnoreCase))
                       {
                            targetPosition = i;
                            break; //we've got what we need. exit loop
                      }
                    }

                    //iterate and skip the column position if exist

                  

                    for (int i = 0; i < split.Length; i++)
                    {
                        if (targetPosition != null && i == targetPosition.Value) continue;

                        collected.Add(split[i]);

                    }

                   lines.Add(string.Join(",", collected));

                    
                
            }
        }
        using (StreamWriter writer = new StreamWriter(path, false))
        {
            foreach (string line in lines)
                writer.WriteLine(line);
        }

Upvotes: 0

JuanR
JuanR

Reputation: 7803

Assuming we ignore the subtleties of writing CSV, this should work:

public void RemoveColumnByIndex(string path, int index)
{
    List<string> lines = new List<string>();
    using (StreamReader reader = new StreamReader(path))
    {
        var line = reader.ReadLine();
        List<string> values = new List<string>();                
        while(line != null)
        {
            values.Clear();
            var cols = line.Split(',');
            for (int i = 0; i < cols.Length; i++)
            {
                if (i != index)
                    values.Add(cols[i]);
            }
            var newLine = string.Join(",", values);
            lines.Add(newLine);
            line = reader.ReadLine();
        }
    }

    using (StreamWriter writer = new StreamWriter(path, false))
    {
        foreach (var line in lines)
        {
            writer.WriteLine(line);
        }
    }

}

The code essentially loads each line, breaks it down into columns, loops through the columns ignoring the column in question, then puts the values back together into a line.

This is an over-simplified method, of course. I am sure there are more performant ways.

Upvotes: 4

Related Questions