2017-10-09 99 views
1

我想删除具有特定值的列。下面的代码是我用来删除一行的代码。我可以将其撤消以删除列吗?使用c删除CSV文件中的列#

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

什么是标准来选择列删除?索引?这种看起来像你已经开始修改代码做你想做的事情,但你卡住了。这是否准确? – JuanR

+0

你是什么意思删除列? –

+0

我想要使用的标准是索引或列的名称(如果可能的话)。是的,我卡住了 – Mylan

回答

1

假设我们忽略写CSV的精妙之处,这应该工作:

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

} 

的代码基本上是加载的每一行,将其分解成列,通过忽略的问题列列循环,然后将这些值重新组合成一行。

当然这是一种过于简化的方法。我相信有更多高性能的方法。

0

要按名称删除列,这里对代码示例稍作修改。

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