c# - Picking specific numbers out of text file and assign to variable -
someone here @ works needs calculations done numbers within text file. know how calculation haven't worked text file before. spent night reading , wrote little first number needed doesn't work.
so here example of file.
so first number comes after fsd: 0.264
need read number , save variable. number different per file. need first 3.4572
number read variable. , last number of column don't see here example can last 1 shown in image of 3.3852
read , saved variable.
maybe i'm making harder needs playing around with
public partial class frmtraveltime : form { string file = ""; public frmtraveltime() { initializecomponent(); } private void button1_click(object sender, eventargs e) { dialogresult result = openfiledialog1.showdialog(); if (result == dialogresult.ok) { file = openfiledialog1.filename; } } private void button2_click(object sender, eventargs e) { var slines = file.readalllines(file) .where(s => !s.startswith("fsd:")) .where(s => !string.isnullorwhitespace(s)) .select(s => new { svalue = regex.match(s, "(?<=s)[\\d.]*").value, }) .toarray(); string value = ""; (int = 0; < slines.length; i++) { if (slines[i].svalue == "") { value = (slines[i].svalue); } } } }
edit @ramankingdom
so if see here lines have ending column of 0.00
until 3.0164 7793 1 0 0.159 0.02
so i'd do, edit did skip has column 0.00
in last column , make first non 0.00
info.firstvalue
in case 3.0164
now tried on own , used var data = lines.where(line => (!line.contains(data_start_point_identifier) && !line.contains(fsd__line_identifier) && !line.endswith("0.00"))).tolist();
but breaks info.startvalue
, data = datawithavgvolts[datawithavgvolts.count - 1].split(splitter);
so i'd figured i'd check you. tried keep getting invalid data error on info.startvalue
fileinfo info = new fileinfo(); var lines = file.readalllines(row.cells["colfilelocation"].value.tostring()); var fsdline = lines.firstordefault(line => line.contains(fsd__line_identifier)); info.fsd = fsdline.substring(fsdline.indexof(fsd_identifier) + fsd_identifier.length, 7); var datawithavgvolts = lines.skipwhile(line => !line.contains(data_start_point_identifier)).tolist(); int index =1; while(index < datawithavgvolts.count()) { var data = datawithavgvolts[index].split(splitter); if(data.count() >1) { if(!convert.todouble(data[data.count()-1]) == 0) { //set start info break; } } index++; }
the reverse loop can run set end value
here code
class program { const string fsd_identifier = "fsd:"; const string fsd__line_identifier = "drilling data"; const string data_start_point_identifier = "avg_volts"; static readonly char[] splitter = {' ','\t'}; static void main(string[] args) { var result = getfileinfo("c:\\sample.txt"); } private static myfileinfo getfileinfo(string path) { myfileinfo info = new myfileinfo(); try { var lines = file.readalllines(path); var fsdline = lines.firstordefault(line => line.contains(fsd__line_identifier)); info.fsd = fsdline.substring(fsdline.indexof(fsd_identifier) + fsd_identifier.length, 10); // take lenght can specify or own logic var datawithavgvolts = lines.skipwhile(line => !line.contains(data_start_point_identifier)).tolist<string>(); if (datawithavgvolts.count() > 1) { var data = datawithavgvolts[1].split(splitter); info.startvalue = convert.todouble(data[0]); data = datawithavgvolts[datawithavgvolts.count-1].split(splitter); info.endvalue = convert.todouble(data[0]); } } catch (exception ex) { //logging here } return info; } } public class myfileinfo { public string fsd; public double startvalue; public double endvalue; }
Comments
Post a Comment