c# - Parsing text file separated by comma -


i developing application read text file , plot graph. text file

#pattern name item1, item2, item3, gx1, gy1, gz1  115, 80, 64, 30.752, 27.587, 15.806  195, 151, 130, 108.983, 102.517, 66.353  94, 123, 156, 43.217, 50.874, 93.700  88, 108, 65, 26.158, 37.980, 17.288  130, 129, 177, 68.096, 66.289, 127.182  100, 190, 171, 71.604, 119.764, 122.349  ......................................... ........................................ #pattern name2 item1, item2, item3, gx1, gy1, gz1  115, 80, 64, 30.752, 27.587, 15.806  195, 151, 130, 108.983, 102.517, 66.353  94, 123, 156, 43.217, 50.874, 93.700  88, 108, 65, 26.158, 37.980, 17.288  130, 129, 177, 68.096, 66.289, 127.182  100, 190, 171, 71.604, 119.764, 122.349 

etc.

i need value gx1,gy1 . planned read them counting spaces , commas (3 each). sounds in appropriate? there optimized or more logic that?

you should use existing csv-parser this.

however, assuming name of pattern unique , want acess later:

var gxdict = new dictionary<string, list<tuple<double, double>>>(); list<tuple<double, double>> currentgxlist = null; foreach (string line in file.readlines(filepath)) {     if (line.startswith("#pattern"))     {         string[] headers = line.split(new[] { ',' }, stringsplitoptions.removeemptyentries);         string patternfield = headers.first();         string pattername = string.join(" ", patternfield.split().skip(1).take(2));         list<tuple<double, double>> gxlist = null;         if (gxdict.trygetvalue(pattername, out gxlist))             currentgxlist = gxlist;         else         {             currentgxlist = new list<tuple<double, double>>();             gxdict.add(pattername, currentgxlist);         }     }     else     {         if (currentgxlist != null)         {             string[] values = line.split(new[] { ',' }, stringsplitoptions.removeemptyentries);             double gx1;             double gy1;             string gx1str = values.elementatordefault(3);             string gy1str = values.elementatordefault(4);             if (double.tryparse(gx1str, out gx1) && double.tryparse(gx1str, out gy1))             {                 currentgxlist.add(tuple.create(gx1, gy1));             }         }     } } 

// can access in way:

list<tuple<double, double>> nameitem1vals = gxdict["name item1"]; foreach (var xy in nameitem1vals)     console.writeline("gx1: {0} gy1: {1}", xy.item1, xy.item2); 

// or in loop:

foreach (var kv in gxdict) {     string pattern = kv.key;     console.writeline("pattern: {0}", pattern);     foreach (var xy in nameitem1vals)         console.writeline("gx1: {0} gy1: {1}", xy.item1, xy.item2); } 

note avoid linq when comes io need event handling on file level , when try parse input cause exceptions.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -