2016-08-12 5 views
1

私のCSV内で1つの列のみを編集しようとしています。しかし、コードはファイルに影響を与えていないようです。変更しようとしているのは、4番目の列のデータをカンマで区切って変更することです。Csvファイルの1つの列を編集する

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

現在のコードには多少の誤差があります。
最大のものはcurrentLineからfileNameへの割り当てです。これはもちろん、行をループしたい場合は意味がありません。だから、読んだ行の上に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()); 

このコードを書く際に何か問題が発生しました。カンマで区切られた5列目のデータだけで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

これは私の問題を解決してくれてありがとうございます。しかし、私は更新されたファイルをチェックすると、編集された5番目の列しか表示されません。 IDはすべての列+更新された列を表示するようにします – user5813072

+0

はい、私はそれについて疑問に思っていました。行全体を書き換えたい場合は、コードの2番目の部分が必要です。それが不明な場合は、私は現在のものを置き換えることができます – Steve

+1

非常にすべての作業をありがとうございます。よろしくお願いいたします。 – user5813072

1

while ((currentLine = fileName) != null)異なるコードが必要後者の場合は、私が代わりにしばらくのループの

としてそれを記述しますラインは常にtrueにすると無限ループ

を行いますcurrentLine =ファイル名を設定します

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

関連する問題