如何将用 ~ 分隔的文本文件解析为 VB.Net 中的数据表或数组
How to Parse Text File delimited with ~ into a Datatable or Array in VB.Net
我正在尝试将文本文件解析为数组或数据表,其中以 D、O 和 L 开头的行是一行数据。
永远不会超过 1 "L" 行。
我想将其放入数据表或二维数组中,其中列 header 名称(位置)为
- 日期 {D3}
- 客户姓名{O2}
- 地址{O3}
- 城市{O7}
- 状态{O8}
- 邮政编码 {O9}
- 参考编号 {D17}
- 金额{D20}
我试过了
TextFieldParser("C:\Users\MyAccount\test.txt")
FileReader.SetDelimiters("~")
但我不明白如何处理输出。有什么想法吗?
B~AAA~~12/03/19~12/03/19~1~428.51~APV~REF~K8~~
D~AAA~~12/03/19~12/03/19~APV~REF~N~REFUNDCIS~~12/03/19~0~N~N~Y~~~0000244909~~~72.90~~~00~N~0~12/03/19~0~12/03/19~12/03/19~0~K8~~~N~N~0~
O~JOHN DOE~ 1000 NOAKY LN ~~~~DETROIT~MI~31000~~~
L~01~141011~000~00000~000~00~000~~REFUND0000244909JOHN DOE~72.90~N~N~~~N~
D~AAA~~12/03/19~12/03/19~APV~REF~N~REFUNDCIS~~12/03/19~0~N~N~Y~~~0000404236~~~101.42~~~00~N~0~12/03/19~0~12/03/19~12/03/19~0~K8~~~N~N~0~
O~BRUCE DOE~UNIT 1 1000 E MICHIGAN AVE ~~~~DETROIT~MI~31000~~~
L~01~141011~000~00000~000~00~000~~REFUND0000404236BRUCE DOE~101.42~N~N~~~N~
D~AAA~~12/03/19~12/03/19~APV~REF~N~REFUNDCIS~~12/03/19~0~N~N~Y~~~0000436750~~~180.00~~~00~N~0~12/03/19~0~12/03/19~12/03/19~0~K8~~~N~N~0~
O~JOEL DOE~ 100 MICHIGAN AVE ~~~~DETROIT~MI~31000~~~
L~01~141011~000~00000~000~00~000~~REFUND0000436750JOEL DOE~180.00~N~N~~~N~
D~AAA~~12/03/19~12/03/19~APV~REF~N~REFUNDCIS~~12/03/19~0~N~N~Y~~~0000448122~~~74.19~~~00~N~0~12/03/19~0~12/03/19~12/03/19~0~K8~~~N~N~0~
O~JOHN DOE~ 100 MICHIGAN AVE ~~~~DETROIT~MI~31000~~~
L~01~141011~000~00000~000~00~000~~REFUND0000448122JOHN DOE~74.19~N~N~~~N~
首先我从 MS 文档中获取代码 https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser?view=netcore-3.1
我需要知道数据表中需要多少列。
Private Sub OpCode1()
Dim maxColumnCount As Integer
Using MyReader As New TextFieldParser("C:\Users\xxx\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
If currentRow.Count > maxColumnCount Then
maxColumnCount = currentRow.Count
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
MessageBox.Show(maxColumnCount.ToString)
End Sub
获得所需的列数后,我创建了一个 DataTable
并添加了必要的列数。然后,在示例指示您处理该行的地方,我将该行添加到 DataTable
。最后,我在 DataGridView
.
中显示了 DataTable
Private Sub OPCode()
Dim dt As New DataTable
For i = 1 To 38
dt.Columns.Add(i.ToString)
Next
Using MyReader As New TextFieldParser("C:\Users\xxx\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
dt.Rows.Add(currentRow)
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
DataGridView1.DataSource = dt
End Sub
更具体到您的数据...
Private Sub OpCode()
Dim dt As New DataTable
dt.Columns.Add("Date", GetType(Date))
dt.Columns.Add("Customer Name", GetType(String))
dt.Columns.Add("Address", GetType(String))
dt.Columns.Add("City", GetType(String))
dt.Columns.Add("State", GetType(String))
dt.Columns.Add("Zipcode", GetType(String))
dt.Columns.Add("RefID", GetType(String))
dt.Columns.Add("Amount", GetType(Decimal))
Dim DataDate As Date
Dim RefID As String = ""
Dim Amount As Decimal
Using MyReader As New TextFieldParser("C:\Users\maryo\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
If currentRow(0) = "D" Then
DataDate = CDate(currentRow(3))
RefID = currentRow(17)
Amount = CDec(currentRow(20))
End If
If currentRow(0) = "O" Then
dt.Rows.Add({DataDate, currentRow(1), currentRow(2), currentRow(6), currentRow(7), currentRow(8), RefID, Amount})
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
DataGridView1.DataSource = dt
End Sub
我假设D行适用于后面的O行。我将D行的数据保存在3个变量中,在读取下一行时使用。
记住集合(包括数组)在 .net 中是基于零的。
我正在尝试将文本文件解析为数组或数据表,其中以 D、O 和 L 开头的行是一行数据。
永远不会超过 1 "L" 行。
我想将其放入数据表或二维数组中,其中列 header 名称(位置)为
- 日期 {D3}
- 客户姓名{O2}
- 地址{O3}
- 城市{O7}
- 状态{O8}
- 邮政编码 {O9}
- 参考编号 {D17}
- 金额{D20}
我试过了
TextFieldParser("C:\Users\MyAccount\test.txt")
FileReader.SetDelimiters("~")
但我不明白如何处理输出。有什么想法吗?
B~AAA~~12/03/19~12/03/19~1~428.51~APV~REF~K8~~
D~AAA~~12/03/19~12/03/19~APV~REF~N~REFUNDCIS~~12/03/19~0~N~N~Y~~~0000244909~~~72.90~~~00~N~0~12/03/19~0~12/03/19~12/03/19~0~K8~~~N~N~0~
O~JOHN DOE~ 1000 NOAKY LN ~~~~DETROIT~MI~31000~~~
L~01~141011~000~00000~000~00~000~~REFUND0000244909JOHN DOE~72.90~N~N~~~N~
D~AAA~~12/03/19~12/03/19~APV~REF~N~REFUNDCIS~~12/03/19~0~N~N~Y~~~0000404236~~~101.42~~~00~N~0~12/03/19~0~12/03/19~12/03/19~0~K8~~~N~N~0~
O~BRUCE DOE~UNIT 1 1000 E MICHIGAN AVE ~~~~DETROIT~MI~31000~~~
L~01~141011~000~00000~000~00~000~~REFUND0000404236BRUCE DOE~101.42~N~N~~~N~
D~AAA~~12/03/19~12/03/19~APV~REF~N~REFUNDCIS~~12/03/19~0~N~N~Y~~~0000436750~~~180.00~~~00~N~0~12/03/19~0~12/03/19~12/03/19~0~K8~~~N~N~0~
O~JOEL DOE~ 100 MICHIGAN AVE ~~~~DETROIT~MI~31000~~~
L~01~141011~000~00000~000~00~000~~REFUND0000436750JOEL DOE~180.00~N~N~~~N~
D~AAA~~12/03/19~12/03/19~APV~REF~N~REFUNDCIS~~12/03/19~0~N~N~Y~~~0000448122~~~74.19~~~00~N~0~12/03/19~0~12/03/19~12/03/19~0~K8~~~N~N~0~
O~JOHN DOE~ 100 MICHIGAN AVE ~~~~DETROIT~MI~31000~~~
L~01~141011~000~00000~000~00~000~~REFUND0000448122JOHN DOE~74.19~N~N~~~N~
首先我从 MS 文档中获取代码 https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser?view=netcore-3.1 我需要知道数据表中需要多少列。
Private Sub OpCode1()
Dim maxColumnCount As Integer
Using MyReader As New TextFieldParser("C:\Users\xxx\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
If currentRow.Count > maxColumnCount Then
maxColumnCount = currentRow.Count
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
MessageBox.Show(maxColumnCount.ToString)
End Sub
获得所需的列数后,我创建了一个 DataTable
并添加了必要的列数。然后,在示例指示您处理该行的地方,我将该行添加到 DataTable
。最后,我在 DataGridView
.
DataTable
Private Sub OPCode()
Dim dt As New DataTable
For i = 1 To 38
dt.Columns.Add(i.ToString)
Next
Using MyReader As New TextFieldParser("C:\Users\xxx\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
dt.Rows.Add(currentRow)
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
DataGridView1.DataSource = dt
End Sub
更具体到您的数据...
Private Sub OpCode()
Dim dt As New DataTable
dt.Columns.Add("Date", GetType(Date))
dt.Columns.Add("Customer Name", GetType(String))
dt.Columns.Add("Address", GetType(String))
dt.Columns.Add("City", GetType(String))
dt.Columns.Add("State", GetType(String))
dt.Columns.Add("Zipcode", GetType(String))
dt.Columns.Add("RefID", GetType(String))
dt.Columns.Add("Amount", GetType(Decimal))
Dim DataDate As Date
Dim RefID As String = ""
Dim Amount As Decimal
Using MyReader As New TextFieldParser("C:\Users\maryo\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
If currentRow(0) = "D" Then
DataDate = CDate(currentRow(3))
RefID = currentRow(17)
Amount = CDec(currentRow(20))
End If
If currentRow(0) = "O" Then
dt.Rows.Add({DataDate, currentRow(1), currentRow(2), currentRow(6), currentRow(7), currentRow(8), RefID, Amount})
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
DataGridView1.DataSource = dt
End Sub
我假设D行适用于后面的O行。我将D行的数据保存在3个变量中,在读取下一行时使用。
记住集合(包括数组)在 .net 中是基于零的。