c# Add a Column to end of CSV file

后端 未结 4 1156
深忆病人
深忆病人 2020-12-20 22:05

Trying to add a column to the end of a cvs file, which will just count up the number of lines (or can all be the same number it doesn\'t really mater) as the main aim is to

4条回答
  •  伪装坚强ぢ
    2020-12-20 22:20

    Why so many materializations ReadAllLines(), .ToList()? Why not just

    String filePath = @"C:/CSV/test.csv";
    
    var csv = File.ReadLines(filePath) // not AllLines
      .Select((line, index) => index == 0 
         ? line + ",Column 4"
         : line + "," + index.ToString())
      .ToList(); // we should write into the same file, that´s why we materialize
    
    File.WriteAllLines(filePath, csv);
    

提交回复
热议问题