let's have text file this
<pre>---------------- hpa m c --------------------- 1004.0 28 13.6 1000.0 62 16.2 998.0 79 17.2 992.0 131 18.0 <pre>---------------- sometext here 1000.0 10 10.6 1000.0 10 11.2 900.0 10 12.2 900.0 100 13.0 <aaa>----------------
how can create array in c# reads text file line number 5 (1004.0
) before line starts string <pre>-
i used string[] lines = system.io.file.readalllines(filepath);
make each line in array problem want numbers of first section in array in order separate them later 3 arrays (hpa, m, c) .
here's possible solution. it's way more complicated should be, should give idea of possible mechanisms further refine data.
string[] lines = system.io.file.readalllines("test.txt"); list<double> results = new list<double>(); foreach (var line in lines.skip(4)) { if (line.startswith("<pre>")) break; regex numberreg = new regex(@"\d+(\.\d){0,1}"); //will find number ending in ".x" - it's primitive, , won't work 0.01, no such data showed in example var result = numberreg.matches(line).cast<match>().firstordefault(); //use first number each line. use cast<match>().skip(1).firstordefault second, , on... if (result != null) results.add(convert.todouble(result.value, system.globalization.cultureinfo.invariantculture)); //note use of invariantculture, otherwise may need worry , or . in numbers }
Comments
Post a Comment