1

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);
}
8
  • What's the criteria to select the column to remove? An index? This kind of looks like you have already started to modify the code to do what you want but you are stuck. Is that accurate? Commented Oct 9, 2017 at 14:26
  • What do you mean by remove column? Commented Oct 9, 2017 at 14:27
  • The criteria I want use is the index or the name of the column if that's possible. And yes i'm stuck Commented Oct 9, 2017 at 14:28
  • 1
    Well as long as the name matches a header you will know which index of your split array you dont need any more... Commented Oct 9, 2017 at 14:29
  • How can I remove it then? Commented Oct 9, 2017 at 14:31

2 Answers 2

4

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.

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

Comments

0

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);
        }

Comments

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.