【问题标题】:Search each word in textbox from textfile and add to datatable从文本文件中搜索文本框中的每个单词并添加到数据表中
【发布时间】:2017-10-11 17:04:33
【问题描述】:

我需要从文本文件列表中搜索单词,并且每个单词在文本框中用空格或分号分隔。将行添加到数据表并将它们显示到数据网格。问题是表上只显示最后一个单词。提前致谢。

    Sub test(ByVal strtofind As String)
    Dim tmp2Table As DataTable = New DataTable

    tmp2Table.Columns.Add("SN", GetType(String))
    tmp2Table.Columns.Add("Dataset", GetType(String))
    tmp2Table.Columns.Add("Param", GetType(String))
    tmp2Table.Columns.Add("Value", GetType(String))

    Dim strTemp() As String
    Dim lines() As String
    Dim strline As String = ""

    Dim fileList = Directory.GetFiles("C:\Users\sterc\Desktop\Traceview\Complete\", "*.txt", False)
    Dim sb = New StringBuilder()
    Dim result As String = String.Empty
    Dim value As String = String.Empty
    Dim s As String = txtParamSearch.Text
    Dim str As String = ""
    Dim strdataset As String = ""
    Dim strParam As String = ""
    Dim strParamResult As String = ""
    Dim strSN As String = ""

    For Each fileName In fileList
        lines = File.ReadAllLines(fileName)
        Dim intTotalLines As Integer = lines.Length
        ' Split string based on spaces.
        For intCounter = 1 To intTotalLines - 1
            strline = lines(intCounter)
            If (Regex.IsMatch(strline, "----- Test_") And Regex.IsMatch(strline, ", Started At ")) Then

                strTemp = strline.Split(" ")
                strdataset = strTemp(3).TrimEnd(",")
            End If
            If Regex.IsMatch(strline, "Reported module serial number:") Then
                strTemp = Regex.Split(strline, ": ")
                strSN = strTemp(1)
            End If
            Dim strParamtofind As String = "\b" & strtofind & "\b\s+(\w+)"

            For Each a As Match In Regex.Matches(strline, strParamtofind, RegexOptions.IgnoreCase)

                tmp2Table.Rows.Add(strSN, strdataset, a.Groups(0).Value, a.Groups(1).Value)

            Next

        Next

    Next
    DataGridView1.DataSource = tmp2Table

End Sub

Sub strArr()

    Dim s As String = txtParamSearch.Text
    Dim str As String = ""

    Dim words As String() = s.Split(New [Char]() {";"c})

    Dim word As String
    For Each word In words
        test(word)
    Next

End Sub

另一件事是显示整数或四舍五入。假设如下图。(带小数点)


我需要从文本文件中搜索数据,每个文件都包含以下行。然后在它下面是我可能在文本框中列出的数据以查找。 假设从我需要查找的文本框中,

  • SOA_MinCurrent_EDATA;
  • Isoa_OPSL_initial

列出的两个中的每一个都将搜索文件夹下列出的每个文本文件。然后逐行显示它们。就像上面的截图一样。问题是只显示字符串搜索和数字四舍五入。


 Testing tune_rf_pwr-rm-f-tpt ----- Test_TxLaserPwrTune, Started At   9/16/2017 5:25:00 PM
 more lines here....
 ...................
 ---tx_laser_pwr_tune_Params    Isoa_OPSL,Isoa_OPSH,laser_power_sp
 ---laser_power_target  2.5
 ---laser_power_initial 2.45000004768372
 ---Isoa_OPSL_initial   2.67
 ---Isoa_OPSH_initial   2.67
 ---laser_power_sp_initial  2.67
 ---Isoa_OPSL   2.67
 ---Isoa_OPSH   2.67
 ---laser_power_sp  2.67
 ---laser_pwr_tune  2.45000004768372
 ---laser_power_target056   2.46
 ---SOA_MinCurrent_EDATA    23.7778471526403
 ---SOA_MinCurrent_Chan 22
 ---SOA_MaxCurrent_EDATA    45.1157734448841
 ---SOA_MaxCurrent_Chan 98
 ---SOA_Current_initial[056]    57.082
 ---SOA_Current_initial[022]    49.389
 ---SOA_Current_initial[098]    49.389
 ---laser_power 2.46
 ---SOA_Current056  57.082
 ---SOA_Current022  49.389
 ---SOA_Current098  68.437
 ---tune_rf_sp_data:tx_laser_pwr_tune_Params    Isoa_OPSL,Isoa_OPSH,laser_power_sp
 ---tune_rf_sp_data:laser_power_target  2.5
 ---tune_rf_sp_data:laser_power_initial 2.45000004768372
 ---tune_rf_sp_data:Isoa_OPSL_initial   2.67
 ---tune_rf_sp_data:Isoa_OPSH_initial   2.67
 ---tune_rf_sp_data:laser_power_sp_initial  2.67
 ---tune_rf_sp_data:Isoa_OPSL   2.67
 ---tune_rf_sp_data:Isoa_OPSH   2.67
 ---tune_rf_sp_data:laser_power_sp  2.67
 ---tune_rf_sp_data:laser_pwr_tune  2.45000004768372
 ---tune_rf_sp_data:laser_power_target056   2.46
 ---tune_rf_sp_data:SOA_MinCurrent_EDATA    23.7778471526403
 ---tune_rf_sp_data:SOA_MinCurrent_Chan 22
 ---tune_rf_sp_data:SOA_MaxCurrent_EDATA    45.1157734448841
 ---tune_rf_sp_data:SOA_MaxCurrent_Chan 98
 ---tune_rf_sp_data:SOA_Current_initial[056]    57.082
 ---tune_rf_sp_data:SOA_Current_initial[022]    49.389
 ---tune_rf_sp_data:SOA_Current_initial[098]    49.389
 ---tune_rf_sp_data:laser_power 2.46
 ---tune_rf_sp_data:SOA_Current056  57.082
 ---tune_rf_sp_data:SOA_Current022  49.389
 ---tune_rf_sp_data:SOA_Current098  68.437

【问题讨论】:

  • 也许,不要对单词使用字符串数据类型,而是尝试使用列表(字符串),并使用循环来处理每个分号拆分,将它们添加到列表中,然后循环数据表行列表

标签: vb.net


【解决方案1】:

如果您提供更多信息我可以帮助您,我在这里有点困惑。

您有多个文件,其中一些文件用空格分隔每列的值并且文件内没有任何分号,而其他文件用分号分隔每列的值并且文件内没有空格对吗?

如果上一个问题是正确的,并且文件中的每一行都包含将填充每一行的信息,那么我将使用:

Dim r as StreamReader as New StreamReader(FilePath)
Dim Line as String

Do While StreamReader.Peek() > -1 //Go through the file until EOF

Line = r.readline() //Read the line and put it into Line
If InStr(Line, " ") Then  //Search for spaces inside Line
//This file is separated by spaces, put the code to add to the table.
Else If InStr(Line, ";") Then //search for semicolons inside line.
//This file is separated by semicolons, put the code to add to the table.
End If

Loop

我希望这会有所帮助。如果没有,请向我们提供更多细节。

【讨论】:

  • 嗨,Alvaro,我已经添加了详细信息。
  • 嗨,Alvaro,我已经通过将数据表放在 button_click 上解决了这个问题。现在完美运行。感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多