16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#でCSV処理するには、Tupleが楽

Last updated at Posted at 2015-06-11

表題の通りですが、サンプル書いてみます。
※例外などは省いて極力シンプルなものにしています。
※3列のCSVで、1列目に数値、2,3列目が文字列を想定しています。


var filePath = @"xxxxx.csv";
var separator = new[] { ',' };
var data = new System.Collections.Generic.List<System.Tuple<int, string, string>>();

foreach (var line in System.IO.File.ReadLines(filePath)) {
  var cols = line.Split(separator);

  data.Add(new System.Tuple<int, string, string>(int.Parse(cols[0]), cols[1], cols[2]));
}

あとは、上記でいえばdata変数をLinqで自由に解析できるのも魅力ですね。
※どちらかといえば、そちらの方が楽しいw

16
17
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?