2016-08-12 101 views
1

我想编辑我的csv中只有一列。但是代码似乎不影响文件。我试图做出的改变是改变为用逗号分隔第四列数据。编辑Csv文件中的一列

class Program 
{ 
    static void Main(string[] args) 
    { 
     var filePath = Path.Combine(Directory.GetCurrentDirectory(), "kaviaReport 02_08_2016.csv"); 
     var fileContents = ReadFile(filePath); 
     foreach (var line in fileContents) 
     { 
      Console.WriteLine(line); 
     } 

     Console.WriteLine("Press any key to exit..."); 
     Console.ReadKey(); 
    } 

    public static IList<string> ReadFile(string fileName) 
    { 
     var results = new List<string>(); 
     int lineCounter = 0; 
     string currentLine = string.Empty; 
     var target = File 
.ReadAllLines(fileName); 
     while ((currentLine = fileName) != null)//while there are lines to read 
     { 
      List<string> fielded = new List<string>(currentLine.Split(',')); 
      if (lineCounter != 0) 
      { 
       //If it's not the first line 
       var lineElements = currentLine.Split(',');//split your fields into an array 
       var replace = target[4].Replace(' ', ',');//replace the space in position 4(field 5) of your array 
       results.Add(replace); 
       //target.WriteAllLines(string.Join(",", fielded));//write the line in the new file 

      } 

      lineCounter++; 
      File.WriteAllLines(fileName, target); 


     } 

     return results; 
    } 
} 
+2

'while((currentLine = fileName)!= null)'......小心解释? – grek40

+0

如果你没有任何限制尝试库:CSVHelper(https://joshclose.github.io/CsvHelper/)这个任务变得更加微不足道。 – Floremin

回答

1

当前的代码有一些错误。
最大的是currentLinefileName的分配。如果你想循环播放,这当然是没有意义的。所以你需要一个阅读线的foreach。
然后在循环内部,您应该使用变量lineElements获取拆分currentLine后可用的5列。
最后,文件的重写超出循环范围,并应使用结果列表。

// Loop, but skip the first line.... 
foreach(string currentLine in target.Skip(1)) 
{ 
    // split your line into an array of strings 
    var lineElements = currentLine.Split(','); 

    // Replace spaces with commas on the fifth column of lineElements 
    var replace = lineElements[4].Replace(' ', ','); 

    // Add the changed line to the result list 
    results.Add(replace); 
} 

// move outside the foreach loop the write of your changes 
File.WriteAllLines(fileName, results.ToArray()); 

编写此代码时出现了一些问题。目前还不清楚,如果你想重写CSV文件,只用第五列中的数据用逗号扩展,或者如果你想在这里重写整行(也是列0,1,2,3,4等)。后一种情况下,你需要一个不同的代码

// Replace spaces with commas on the fifth column of lineElements 
// And resssign the result to the same fifth column 
lineElements[4] = lineElements[4].Replace(' ', ','); 

// Add the changed line to the result list putting the comma 
// between the array of strings lineElements 
results.Add(string.Join(",", lineElements); 
+0

非常感谢你的解决方案,这已经解决了我的问题。但是当我检查更新的文件时,它只显示已编辑的第五列。编号喜欢查看所有列+更新列 – user5813072

+0

是的,我想知道这一点。如果你想重写整行,你需要代码的第二部分。如果不清楚我可以替换当前的一个 – Steve

+1

非常感谢你的工作。非常感谢 – user5813072

1

while ((currentLine = fileName) != null)将设置currentLine =文件名,这将使线路始终为真,并进行无限循环

我将它写成一个for循环,而不是同时

public static IList<string> ReadFile(string fileName) 
    { 
     var target = File.ReadAllLines(fileName).ToList(); 
     // i = 1 (skip first line) 
     for (int i = 1; i < target.Count; i++) 
     { 
      target[4] = target[4].Replace(' ', ','); //replace the space in position 4(field 5) 
     } 
     File.WriteAllLines(fileName, target); 
     // Uncomment the RemoveAt(0) to remove first line 
     // target.RemoveAt(0); 
     return target; 
    } 
+0

感谢你似乎正在做我想做的事情。然而它只影响每列中的第5行而不仅仅是第5列 – user5813072