开发者

fancy way to load contents of a CSV file into a dictionary<string,string> in C#

开发者 https://www.devze.com 2022-12-13 15:07 出处:网络
I know how to do this via streamreader and read to the end of line, but was curious if there was a fancier way of going it (for the sake of learing).

I know how to do this via streamreader and read to the end of line, but was curious if there was a fancier way of going it (for the sake of learing).

filename: blah.csv

File layout is simple:

"Some234234        ", 234
"blahblha234234    ", 2322

I want to load this into a dictionary (the second part should be a int, but I will parse lat开发者_Go百科er in case of errors).

Can you do this via linq somehow?


You should use the TextFieldParser class in Microsoft.VisualBasic.dll. (There's nothing wrong with using it in C#.

Remember that CSV is a much more complicated file format than you think; both quotes and newlines can be escaped.


CSV can be a lot more complicated than it appears at first. Here is an excellent library for reading CSV files.

http://www.codeproject.com/KB/database/CsvReader.aspx


Untested, but:

static IEnumerable<string> ReadLines(string path) {
    using (var file = File.OpenText(path)) {
        string line;
        while ((line = file.ReadLine()) != null) {
            yield return line;
        }
    }
}

var data = (from line in ReadLines(path)
            select line.Split(','))
           .ToDictionary(
              arr => arr[0].Trim('"', ' '),
              arr => int.Parse(arr[1].Trim()));


foreach (string s in File.ReadAllLines(filename)) {
 var vals = s.Split(',')
 dictionary[vals[0]] = vals[1];
}

No LINQ but this is simple really. Doesn't handle embedded ',''s in the first value though.


 string[] file = File.ReadAllLines(@"C:\temp\dictionary.txt");

      Dictionary<string, string> b = (from p in file
                                     let x = p.Split(',')
                                     select x).ToDictionary(a => a[0], a => a[1]);


You could use File.ReadAllLines(), Select() and ToDictionary() to do this:

var d = File.ReadAllLines(file).Select( l => {
  var split = l.Split(',');
  return new { Key = split[0], Value = split[1] };
} ).ToDictionary( p => p.Key, p => p.Value );

But there are obvious problems here with respect to error handling and robustness, and as you add those, it gets worse and worse. I don't feel there's a particular good reason prefer LINQ or its extension methods here, as it isn't buying you much. The straightforward ways, posted already, are much cleaner.

EDIT: Sid's answer, for example, contains essentially the same code, but written in a much, much cleaner form by avoiding all this "fancy" junk.


This might be overkill in your simple scenario, but for CSV to strongly typed collection conversion, I usually use FileHelpers.

It's a great tool to have in the tool box.

0

精彩评论

暂无评论...
验证码 换一张
取 消