使用 MS Access 解析具有 VBA 的字节数组
Parsing a byte array with VBA using MS Access
我有一个 1 亿个字符的文本文件,我正试图将其导入到 MS Access 数据库中。该文件没有任何换行符,因此它只是一大行文本。我尝试将它加载到一个字符串变量中,但由于大小而无法正常工作。然后我成功地将它加载到一个字节数组中,但我不确定如何按照我需要的方式解析它。该文件具有固定长度的记录,但具有不止一种类型的记录。一种可能是180个字符的数据和220个字符的填充符,另一种可能是100个字符的数据和300个字符的填充符。我想将不同的记录类型放入单独的表中。我正在考虑将数据重写到 400 个字符块中的新文本文件,然后可能使用 Trim 来获取我需要的数据而没有填充。从那里我可以测试线的长度并导入。
这是我的,但它不起作用。
Public Sub modMain_ParseAQTFiles()
Dim bytFile(400) As Byte
Dim intFileIn As Integer
Dim intFileOut As Integer
Dim intFileOut1 As Integer
Dim intFileOut2 As Integer
Dim intFFIn As Integer
Dim intFFOut As Integer
Dim lngBytePos As Long
Dim dblStartChar As Double
Dim lngNoRecs As Long
Dim lngIndex As Long
Dim strFileIn As String
Dim strFileOut1 As String
Dim strFileOut2 As String
Dim strLineOfText As String
Dim strTextLine As String
Dim strUserName As String
'Get username
strUserName = Environ("Username")
'Set file paths
strFileIn = "C:\Users\" & strUserName & "\Desktop\Pooltalk\aqt.txt"
strFileOut1 = "C:\Users\" & strUserName & "\ Desktop\Pooltalk\ AQT_Quartiles_Header-out.txt"
strFileOut2 = "C:\Users\" & strUserName & "\Desktop\Pooltalk \AQT_Quartiles_Detail-out.txt"
'Reads data into byte array
intFFIn = FreeFile
intFFOut = FreeFile
dblStartChar = 1
Open strFileIn For Binary Access Read As #intFFIn
lngNoRecs = LOF(intFFIn) / 400
For lngIndex = 1 To lngNoRecs
Get #intFFIn, dblStartChar, bytFile
strLineOfText = StrConv(bytFile, vbFromUnicode)
Open strFileOut For Binary Access Write As #intFFOut
Put intFFOut, dblStartChar, strLineOfText & vbCrLf
Debug.Print strLineOfText
dblStartChar = dblStartChar + 400
Next lngIndex
Close #intFFIn
Close #intFFOut
End Sub
我很乐意听取是否有人有任何建议可以使它正常工作。谢谢。
编辑:
这是一种记录类型:
1004569 AS20170431360FCE319840701
34个数据字符和366个空格
这是第二种记录类型:
200456906875{06875{06875{06875{06875{06875{07I07I07I07I07I07I40B40B40B40B40B40B0000630000{0000630000{0000630000{0000630000{0000630000{0000630000{48{48{48{48{48{48{05926{05926{05926{05926{05926{05926{01250{01250{01250{01250{01250{01250{06875{06875{06875{06875{06875{06875{16875{16875{16875{16875{16875{16875{
307 个字符和 93 个空格。
这是我的最终代码:
Public Sub modMain_ParseAQTFiles()
Dim intFileIn As Integer
Dim intFileOut1 As Integer
Dim intFFIn As Integer
Dim intFFOut As Integer
Dim lngNoRecs As Long
Dim lngIndex As Long
Dim strFileIn As String
Dim strFileOut1 As String
Dim strUserName As String
Dim strRecord As String
Dim dblStartChar As Double
Dim lngCharNo As Long
strUserName = Environ("Username")
'Set file paths
strFileIn = "C:\Users\" & strUserName & "\Desktop\Pooltalk\aqt.txt"
strFileOut1 = "C:\Users\" & strUserName & "\Desktop\Pooltalk\AQT_Parsed.txt"
strRecord = Space$(400)
dblStartChar = 1
'Reads data into byte array
intFFIn = FreeFile
Open strFileIn For Binary Access Read As #intFFIn
intFFOut = FreeFile
Open strFileOut1 For Binary Access Write As #intFFOut
'Find number of records
lngNoRecs = LOF(intFFIn) / 400
For lngIndex = 1 To lngNoRecs
Get #intFFIn, dblStartChar, strRecord
strRecord = Trim(strRecord)
Put intFFOut, , strRecord & vbCrLf
dblStartChar = dblStartChar + 400
strRecord = Space$(400)
Next lngIndex
Close #intFFIn
Close #intFFOut
MsgBox "Done!"
End Sub
如果所有记录都是 400 个字符长,我会直接将它们读入该长度的字符串变量中。
Dim strRecord As String
Dim x As Long
' Get reads as many characters as are in the target variable
strRecord = Space$(400)
Get #intFFIn, dblStartChar, strRecord
' Find first 0-byte character
x = Instr(strRecord, Chr$(0))
' and trim off the fillers
strRecord = Left$(strRecord, x-1)
请参阅底部的 https://msdn.microsoft.com/VBA/Language-Reference-VBA/articles/get-statement(示例之前)。
我有一个 1 亿个字符的文本文件,我正试图将其导入到 MS Access 数据库中。该文件没有任何换行符,因此它只是一大行文本。我尝试将它加载到一个字符串变量中,但由于大小而无法正常工作。然后我成功地将它加载到一个字节数组中,但我不确定如何按照我需要的方式解析它。该文件具有固定长度的记录,但具有不止一种类型的记录。一种可能是180个字符的数据和220个字符的填充符,另一种可能是100个字符的数据和300个字符的填充符。我想将不同的记录类型放入单独的表中。我正在考虑将数据重写到 400 个字符块中的新文本文件,然后可能使用 Trim 来获取我需要的数据而没有填充。从那里我可以测试线的长度并导入。
这是我的,但它不起作用。
Public Sub modMain_ParseAQTFiles()
Dim bytFile(400) As Byte
Dim intFileIn As Integer
Dim intFileOut As Integer
Dim intFileOut1 As Integer
Dim intFileOut2 As Integer
Dim intFFIn As Integer
Dim intFFOut As Integer
Dim lngBytePos As Long
Dim dblStartChar As Double
Dim lngNoRecs As Long
Dim lngIndex As Long
Dim strFileIn As String
Dim strFileOut1 As String
Dim strFileOut2 As String
Dim strLineOfText As String
Dim strTextLine As String
Dim strUserName As String
'Get username
strUserName = Environ("Username")
'Set file paths
strFileIn = "C:\Users\" & strUserName & "\Desktop\Pooltalk\aqt.txt"
strFileOut1 = "C:\Users\" & strUserName & "\ Desktop\Pooltalk\ AQT_Quartiles_Header-out.txt"
strFileOut2 = "C:\Users\" & strUserName & "\Desktop\Pooltalk \AQT_Quartiles_Detail-out.txt"
'Reads data into byte array
intFFIn = FreeFile
intFFOut = FreeFile
dblStartChar = 1
Open strFileIn For Binary Access Read As #intFFIn
lngNoRecs = LOF(intFFIn) / 400
For lngIndex = 1 To lngNoRecs
Get #intFFIn, dblStartChar, bytFile
strLineOfText = StrConv(bytFile, vbFromUnicode)
Open strFileOut For Binary Access Write As #intFFOut
Put intFFOut, dblStartChar, strLineOfText & vbCrLf
Debug.Print strLineOfText
dblStartChar = dblStartChar + 400
Next lngIndex
Close #intFFIn
Close #intFFOut
End Sub
我很乐意听取是否有人有任何建议可以使它正常工作。谢谢。
编辑:
这是一种记录类型:
1004569 AS20170431360FCE319840701
34个数据字符和366个空格
这是第二种记录类型:
200456906875{06875{06875{06875{06875{06875{07I07I07I07I07I07I40B40B40B40B40B40B0000630000{0000630000{0000630000{0000630000{0000630000{0000630000{48{48{48{48{48{48{05926{05926{05926{05926{05926{05926{01250{01250{01250{01250{01250{01250{06875{06875{06875{06875{06875{06875{16875{16875{16875{16875{16875{16875{
307 个字符和 93 个空格。
这是我的最终代码:
Public Sub modMain_ParseAQTFiles()
Dim intFileIn As Integer
Dim intFileOut1 As Integer
Dim intFFIn As Integer
Dim intFFOut As Integer
Dim lngNoRecs As Long
Dim lngIndex As Long
Dim strFileIn As String
Dim strFileOut1 As String
Dim strUserName As String
Dim strRecord As String
Dim dblStartChar As Double
Dim lngCharNo As Long
strUserName = Environ("Username")
'Set file paths
strFileIn = "C:\Users\" & strUserName & "\Desktop\Pooltalk\aqt.txt"
strFileOut1 = "C:\Users\" & strUserName & "\Desktop\Pooltalk\AQT_Parsed.txt"
strRecord = Space$(400)
dblStartChar = 1
'Reads data into byte array
intFFIn = FreeFile
Open strFileIn For Binary Access Read As #intFFIn
intFFOut = FreeFile
Open strFileOut1 For Binary Access Write As #intFFOut
'Find number of records
lngNoRecs = LOF(intFFIn) / 400
For lngIndex = 1 To lngNoRecs
Get #intFFIn, dblStartChar, strRecord
strRecord = Trim(strRecord)
Put intFFOut, , strRecord & vbCrLf
dblStartChar = dblStartChar + 400
strRecord = Space$(400)
Next lngIndex
Close #intFFIn
Close #intFFOut
MsgBox "Done!"
End Sub
如果所有记录都是 400 个字符长,我会直接将它们读入该长度的字符串变量中。
Dim strRecord As String
Dim x As Long
' Get reads as many characters as are in the target variable
strRecord = Space$(400)
Get #intFFIn, dblStartChar, strRecord
' Find first 0-byte character
x = Instr(strRecord, Chr$(0))
' and trim off the fillers
strRecord = Left$(strRecord, x-1)
请参阅底部的 https://msdn.microsoft.com/VBA/Language-Reference-VBA/articles/get-statement(示例之前)。