我有一个包含制表符分隔值的文本文件,我想将其加载到数组或列表中,以便我可以进一步处理这些值。我已经使用了here提供的解决方案,我相信它非常接近我所需要的,但是在我的文件中,每行的第一个值是日期和时间戳,然后该行上的每个其他值将是双。
对于临时测试我试图将值加载到数据网格视图控件中,但是我收到输入字符串格式不正确的错误。 我需要稍后按日期/时间对值进行排序,所以我肯定需要加载这些值,但我不确定如何实现这一点?
Private Function LoadFile(ByVal filePath As String) As List(Of List(Of Double))
'Look at a text file and load stored values into a list
Dim records As New List(Of List(Of Double))()
For Each line As String In File.ReadAllLines(filePath)
Dim values As New List(Of Double)()
For Each field As String In line.Split(New String() {ControlChars.Tab}, StringSplitOptions.None)
values.Add(Double.Parse(field))
Next
records.Add(values)
Next
Return records
End Function
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
dgvValues.DataSource = LoadFile(strFullPath)
End Sub
Private Sub clbFileList_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles clbFileList.ItemCheck
strFileName = clbFileList.SelectedItem.ToString
strFullPath = strSourcePath + "\" + strFileName
End Sub
以下几行作为我尝试阅读该计划的样本:
12:18:30 02-28-2014 189.233333333333 666.35
12:33:30 02-28-2014 274.716666666667 1111.35
12:48:30 02-28-2014 265.516666666667 1052.9
13:03:00 02-28-2014 253.583333333333 1164.25
答案 0 :(得分:2)
Class myStuff
Public Property theDate As DateTime
Public Property oneValue As Double
Public Property twoValue As Double
' optionally add a constructor to
' create the object with everything at once:
Public Sub New(dt as DateTime, v1 As Double, v2 As Double)
theDate = dt
oneValue = v1
twoValue = v2
End Sub
End Class
其他地方:
Public stuffList As New List(Of MyStuff)
Dim El as MyStuff
' presumably in a loop:
' after you parse the text into date and doubles:
El = New MyStuff(dateRead, val1Read, val2Read)
stuffList.Add El
更紧凑,没有临时元素变量(以上更能说明过程):
stuffList.Add(New MyStuff(dateRead, val1Read, val2Read))