從一行文本中獲取特定數據 (Get specific data from a line of text)


問題描述

從一行文本中獲取特定數據 (Get specific data from a line of text)

我需要從下面的文本行中提取以下數據(粗體)並將其放入數據網格中;

PERS tooldata t_rrt_ja03579:=[TRUE,[ [‑39.643,‑0.001,1025.49],[0.382684,‑0.000130001,‑0.923889,0.000120001]],[200.9,[‑88.1,‑12.6,359.7], [1,0,0,0],29.347,50.927,18.261]];

這一行是從文件中讀取的。我設法修剪了這條線,所以它擺脫了“PERS tooldata”和空格,它給我留下了工具名稱。我將它綁定到代碼中其他地方的數據網格中的數據,這是第 1 步完成的。

我的問題是如何單獨提取粗體值並將它們放入雙重數據聲明中?第一個值塊 (‑39.643,‑0.001,1025.49) 是 X,Y, Z坐標值和第二個(0.382684,‑0.000130001,‑0.923889,0.000120001)分別是Q1,Q2,Q3,Q4。

下面是我的名字

    private void AutoFillToolData(object sender, RoutedEventArgs e)
    {
        // Gives user option to auto populate datagrid
        var AutoFillToolResult = MessageBox.Show("Do you want to auto populate fields?", "Tool Data", MessageBoxButton.YesNo);
        if (AutoFillToolResult == MessageBoxResult.Yes)
        {
            // User directs application to the specified file
            System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
            if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Application looks for specific file and removes unwanted data
                string robotBackupPath = folderBrowser.SelectedPath;
                string allDataPath = robotBackupPath + @"\RAPID\TASK1\SYSMOD\ALL_DATA.sys";
                string[] tLines = File.ReadAllLines(allDataPath);
                List<string> toolDataLines = new List<string>();
                foreach (string tLine in tLines)
                {
                    if (tLine.Contains("PERS tooldata") && !tLine.StartsWith("!"))
                    {
                        if (tLine.Contains("tToolChanger")) continue;
                        if (tLine.Contains("tPointer")) continue;
                        if (tLine.Contains("tHome")) continue;
                        toolDataLines.Add(tLine);
                    }
                }

                foreach (string line in toolDataLines)
                {
                    // Gets the name of the tool
                    ToolData toolData = GetToolNameFromLine(line);

                    // Puts the tool name in the DataGrid 
                    TCPData.Add(toolData);
                }
            }
        }
    }

    private ToolData GetToolNameFromLine(string line)
    {
        // Removes white space at the beggining of line in txt file
        ToolData tooldata = new ToolData();
        string[] spaceSplit = line.Trim().Split(' ');
        string values = spaceSplit[2];

        // Gets Tool Name
        int colonLocation = values.IndexOf(":");
        tooldata.ToolName = values.Substring(0, colonLocation);
        return tooldata;
    }

參考解法

方法 1:

If all the samples you'll have follow the same pattern, extracting those values does not seem difficult:

//First we get all the string after the :=
string tooldata = line.Substring(data.IndexOf(":=") + 2) ;

//Split the string by [        
string[] tooldataArray = tooldata.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries);
//the second and the third strings are what we are interested in
string xyzValue  = tooldataArray[1].Replace(']' ,' ');
string Q1234value = tooldataArray[2].Replace(']', ' ');

If after this you want to get the individual parameters, just splitting by , would do.

Edit

This would extract all the values you want to arrays of double:

string tooldata = data.Substring(data.IndexOf(":=") + 2) ;

string[] tooldataArray = tooldata.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries);
double[] xyzValue  = tooldataArray[1].Replace(']' ,' ')
                    .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => double.Parse(s, CultureInfo.InvariantCulture))
                    .ToArray();
double[] Q1234value = tooldataArray[2].Replace(']', ' ')
                    .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => double.Parse(s, CultureInfo.InvariantCulture))
                    .ToArray();

(by Scott FitzpatrickPikoh)

參考文件

  1. Get specific data from a line of text (CC BY‑SA 2.5/3.0/4.0)

#split #wpf #text #trim #C#






相關問題

將 xml 元素內容拆分為固定行數 (Split xml element content into fix number of lines)

是否有任何標准說明“aba”.split(/a/) 是否應該返回 1,2 或 3 個元素? (Is there any standard which says if "aba".split(/a/) should return 1,2, or 3 elements?)

Cố gắng gọi các phương thức trong phương thức main với biến được khởi tạo trong các phương thức khác (Trying to call methods in main method with variable initialized in other methods)

使用 Java-Regex 與 Regex 成對拆分多行文本 (Split text with Java-Regex in pairs with Regex over several lines)

如何分割字節數組 (How to split a byte array)

String componentsSeparatedByString 做一次 (String componentsSeparatedByString do one time)

從一行文本中獲取特定數據 (Get specific data from a line of text)

(Python)拆分字符串多個分隔符更有效?1) 使用多重替換方法然後使用拆分 2) 使用正則表達式 ((Python) which is more efficient to split a string multiple separators? 1) Using multiple replace method then using split 2) using regular Expressions)

ValueError:發現樣本數量不一致的輸入變量:[2935848、2935849] (ValueError: Found input variables with inconsistent numbers of samples: [2935848, 2935849])

在 Powershell 中拆分和添加字符串 (Splitting and Adding String in Powershell)

在 python 函數中檢查月份的有效性時出錯 (Error in checking validity of month in python function)

如何將 .obj 文件拆分為其他兩個文件(python、open3d)? (How to split a .obj file into two other files (python, open3d)?)







留言討論