在 VB 2015 中将大型导入文本文件拆分为动态 2D 数组



我目前正在尝试编写一种方法来读取充满大量数据的文本文件,并创建一个动态 2d 数组以将每个数值保存在其自己的单元格中。文本文件的数据格式如下

150.00 0.00030739 0.00030023 21.498 0.00024092

150.01 0.00030778

0.00030061 21.497 0.00024122

150.02 0.00030818 0.00030100 21.497 0.00024151

150.03 0.00030857 0.00030138 21.496 0.00024181

150.04 0.00030896 0.00030177 21.496 0.00024210

150.05 0.00030935 0.00030216

21.496 0.00024239

其中空格由 vbTab 表示。这就是我目前所拥有的。

    Dim strfilename As String
    Dim num_rows As Long
    Dim num_cols As Long
    Dim x As Integer
    Dim y As Integer

    strfilename = "Location of folder holding file" & ListBox1.SelectedItem
    If File.Exists(strfilename) Then
        Dim sReader As StreamReader = File.OpenText(strfilename)
        Dim strLines() As String
        Dim strLine() As String
        'Load content of file to strLines array
        strLines = sReader.ReadToEnd().Split(Environment.NewLine)
        'redimension the array
        num_rows = UBound(strLines)
        strLine = strLines(0).Split(vbTab)
        num_cols = UBound(strLine)
        ReDim sMatrix(num_rows, num_cols)
        'Copy Data into the array
        For x = 0 To num_rows
            strLine = strLines(x).Split(vbTab)
            For y = 0 To num_cols
                sMatrix(x, y) = strLine(y).Trim()
            Next
        Next
    End If

当我运行此代码时,我只得到数组第一列中的第一个数字,其他所有内容都丢失了。我需要显示所有值的东西。任何帮助或指导将不胜感激

编辑:这是我看到的图片。

我所看到的

您不需要一次性读取所有数据 - 您可以逐行读取并处理每一行。

我假设数据是机器生成的,以便您知道没有错误。但是,我确实在一行上检查了所需数量的项目。

我复制了您作为示例提供的数据并对其进行了编辑以将空格更改为制表符。

Option Strict On
Option Infer On
Imports System.IO
Module Module1
    Class Datum
        Property Time As Double
        Property X As Double
        Property Y As Double
        Property Z As Double
        Property A As Double
        Sub New(t As Double, x As Double, y As Double, z As Double, a As Double)
            Me.Time = t
            Me.X = x
            Me.Y = y
            Me.Z = z
            Me.A = z
        End Sub
        Sub New()
            ' empty constructor
        End Sub
        Overrides Function ToString() As String
            Return String.Format("(T={0}, X={1}, Y={2}, Z={3}, A={4}", Time, X, Y, Z, A)
            ' if using VS2015, you can use the following line instead:
            ' Return $"T={Time}, X={X}, Y={Y}, Z={Z}, A={A}"
        End Function
    End Class
    Function LoadData(srcFile As String) As List(Of Datum)
        Dim data = New List(Of Datum)
        Using sr As New StreamReader(srcFile)
            While Not sr.EndOfStream()
                Dim thisLine = sr.ReadLine()
                Dim parts = thisLine.Split({vbTab}, StringSplitOptions.RemoveEmptyEntries)
                If parts.Count = 5 Then
                    data.Add(New Datum(CDbl(parts(0)), CDbl(parts(1)), CDbl(parts(2)), CDbl(parts(3)), CDbl(parts(4))))
                End If
            End While
        End Using
        Return data
    End Function
    Sub Main()
        Dim src = "C:temptestdata2.txt"
        Dim myData = LoadData(src)
        For Each datum In myData
            Console.WriteLine(datum.ToString())
        Next
        Console.ReadLine()
    End Sub
End Module

如您所见,如果您使用类来保存数据,则可以有效地为其提供其他方法,例如.ToString()

选项"严格打开"可确保您不执行任何无意义的操作,例如尝试以数字数据类型存储字符串。强烈建议您将"选项严格打开"设置为所有项目的默认值。

选项Infer On让编译器在使用类似Dim k = 1.0时确定你想要的数据类型,所以你不必键入Dim k As Double = 1.0,但请注意,如果你使用Dim k = 1它会推断k是一个整数。

一旦数据位于列表中的类实例中,就可以使用 LINQ 以相当易于阅读的方式处理它,例如,您可以

Console.WriteLine("The average X value is " & (myData.Select(Function(d) d.X).Average()).ToString())

最新更新