使用具有不同固定长度的TextFieldParser

时间:2014-01-15 14:31:24

标签: vb.net parsing text

我的系统正在生成包含未知数据量的文本文件。我有两个单独的代码块,一个用于付款,另一个用于分发。我知道我将至少有一个付款和一个分配(付款有23个字段,宽度不同,分配有12个字段)。

付款字段长度为:{10, 1, 10, 8, 1, 20, 13, 1, 8, 8, 8, 40, 40, 40, 40, 40, 25, 2, 9, 40, 10, 20, 6}

分配字段长度为:{10, 1, 10, 20, 40, 13, 1, 40, 40, 10, 1, 14}

付款字段为400个字符,然后“分配”字段为200个字符。我可以有一个分销字段或许多分配字段以及付款字段,但付款字段不按顺序排列。

付款结构示例[请勿删除空格]我需要维护结构以防万一有值:

00000041285111       20140106EDA0000-001          0000010636317+201401012014010320140106                                        Some Tax Company                     Non testing agency service                                                                                                                                    TEST GROUP INC                         11#####23                            

分布结构的示例[不要移除空间]我需要维护结构以防万一有值:

00000041286111       DA0000-005                                                  0000000016731+                                        666111                                  98552                   

我需要能够通过第一次付款来解析固定长度中的所有值,并以某种方式将字段的长度切换为分配字段,直到我得到所有这些,并且如果它去了则将其切换回来回到付款。

以下是我的代码:

        Using MyReader As New Microsoft.VisualBasic.FileIO.
TextFieldParser("C:\COM_20140103_173912.txt")
        'This field parser gives me the first 400characters with not problems
        MyReader.TextFieldType =
            Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
        MyReader.FieldWidths = {10, 1, 10, 8, 1, 20, 13, 1, 8, 8, 8, 40, 40, 40, 40, 40, 25, 2, 9, 40, 10, 20, 6}
        Dim currentRow As String()
        While Not MyReader.EndOfData
            Try
                currentRow = MyReader.ReadFields()
                Dim currentField As String
                For Each currentField In currentRow
                    MsgBox(currentField)
                Next
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Line " & ex.Message &
                "is not valid and will be skipped.")
            End Try
        End While
    End Using

我从http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.fieldwidths.aspx

获得了此代码

我做过研究,我无法弄清楚如何操纵它来做我需要它做的事情。任何帮助,将不胜感激。我希望我提供了足够的信息,如果你需要我以不同的方式解释,我可以。

谢谢, 约翰

1 个答案:

答案 0 :(得分:2)

这是一个想法。 TextFielParser构造函数还可以将流作为其参数。使用常规StreamReader逐行读取文件,然后使用带有TextFieldParser的 StringReader 根据它的长度处理每一行。类似于以下内容(未经测试):

Imports Microsoft.VisualBasic.FileIO
Imports System.IO

Sub Main
    Using rdr As New StreamReader("C:\COM_20140103_173912.txt")
        Dim currentLine As String = rdr.ReadLine()
        While currrentLine IsNot Nothing
            Dim currentRow As String()

            If currentLine.Length = 400 Then
                currentRow = processDistributionRow(currentLine)
            Else
                currentRow = processPaymentRow(currentLine)
            End If

            If currentRow IsNot Nothing Then
                'Process current set of fields
            End If

            currentLine = rdr.ReadLine()
        End While
    End Using
End Sub

'This method uses a TextFieldParser to process a single line of a file that is passed in
Private Function processDistributionRow(currentLine As String)

    Dim result As String()

    Using strStream As New StringStream(currentLine)
        Using MyReader As New TextFieldParser(strStream)
            MyReader.TextFieldType =  FieldType.FixedWidth
            MyReader.FieldWidths = {10, 1, 10, 8, 1, 20, 13, 1, 8, 8, 8, 40, 40, 40, 40, 40, 25, 2, 9, 40, 10, 20, 6}
            Try
                result = MyReader.ReadFields()
                Dim currentField As String
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
            End Try

        End Using
    End Using

    return result
End Function

'This method uses a TextFieldParser to process a single line of a file that is passed in
Private Function processPaymentRow(currentLine As String)
    Dim result As String()

    Using strStream As New StringStream(currentLine)
        Using MyReader As New TextFieldParser(strStream)
            MyReader.TextFieldType =  FieldType.FixedWidth
            MyReader.FieldWidths = {?, ?, ?}   'Set proper field widths for the payment row here
            Try
                result = MyReader.ReadFields()
                Dim currentField As String
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
            End Try
        End Using
    End Using

    return result
End Function


' Define other methods and classes here
相关问题