如何在VB.NET中解析此文本文件?

时间:2012-01-13 03:36:27

标签: vb.net parsing text

Example Data Link - 抱歉,粘贴后无法正确格式化。

1.
 Dec 01, 2011
 06:00:00 AM
 Dec 01, 2011
 07:05:00 AM
 65
2.11
2.
 Dec 01, 2011
 06:00:00 PM
 Dec 01, 2011
 07:05:00 PM
 65
2.11
3.
 Dec 02, 2011
 06:05:00 AM
 Dec 02, 2011
 07:05:00 AM
 60
1.95

我希望每个单独的行在数组或数据表中都有自己的位置,但我似乎无法让它正常工作。结尾字符必须有不同的东西吗?

代码如下。

    Dim strOutput As String = ""

    '' Demo Data
    'Dim strData As String = "59. Dec 01, 2011 06:05:00 PM Dec 01, 2011 10:05:00 PM 240 80.00"
    'strOutput = +FormatRow(strData)
    '' Demo Data

    Dim sFileName As String = OpenFileDialog.FileName
    If My.Computer.FileSystem.FileExists(sFileName) Then
        Dim srFileReader As System.IO.StreamReader
        Dim sInputLine As String
        srFileReader = System.IO.File.OpenText(sFileName)
        sInputLine = srFileReader.ReadLine()

        Do Until sInputLine Is Nothing
            'strOutput = +FormatRow(sInputLine)
            Dim title As String = srFileReader.ReadLine()
            Dim startTime As DateTime = srFileReader.ReadLine() & " " & srFileReader.ReadLine()
            Dim endTime As DateTime = srFileReader.ReadLine() & " " & srFileReader.ReadLine()
            Dim timeSpan As TimeSpan = endTime.Subtract(startTime)
            Dim minutesTotal As Integer = timeSpan.TotalMinutes
            ' Burn Minutes
            srFileReader.ReadLine()
            Dim billMinutes As Integer = minutesTotal
            Dim billTotal As Double = srFileReader.ReadLine()

            strOutput += ""
        Loop

输出类似

的内容
12/1/2011 6:00:00 AM    12/1/2011 7:05:00 AM    65  65      2.11

1 个答案:

答案 0 :(得分:4)

您已经阅读了文本文件,我认为File.ReadAllLines将是不错的选择。

 Dim str
 Dim fileName = "C:\SampleData.txt"

 Dim lines() = File.ReadAllLines(fileName)

 For i = 0 To lines.GetUpperBound(0) Step 7
  str = String.Format("{0}{1}{2}{3}{4}{5}", 
                     lines(i), lines(i + 1), lines(i + 2), lines(i + 3), 
                     lines(i + 4), lines(i + 5))
  Console.WriteLine(str)
 Next