如何删除 CSV 开头的 NULL 字符串; VBA
How to remove NUL strings at begining of CSV; VBA
我有一个程序可以导出 material 的账单(请参阅 post 中下方的 CSV 示例数据)。
我写了一个 VBA 宏,它使用 FileSystemObject 循环访问用户选择的文件夹,打开 CSV 文件进行读取并使用 ReadAll 将数据读入内存,然后将数据存储并排序 excel.
我遇到的问题是在导出 CSV 文件后,我 运行 CSV 文件中的宏和文本作为几个方括号存储在我的字符串变量(下面代码中的 sTemp)中。我复制了字符串变量的值并将其粘贴到文本编辑器中,它显示为几个包含 "SOH" 的重复黑框。因为数据读取错误导致宏下游失效
如果我打开原始 CSV 文件,前两个字符是黑框,上面写着 "NUL"。
NUL NUL "Pattern","Qty","Size","Material","Description","Length (ft)"
2041,"3","","316L 不锈钢 Sch 10S PE",20
2041,"3","","316L 不锈钢 Sch 10S PE",2.21
5044,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD",
2523,1,"3","Stainless Steel: 316L","316L-SS-SCH10 短半径 90",
2522,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD",
如果我将 CSV 文件保存在 Excel 中并关闭它们,两个 "NUL" 字符就会消失:
"Pattern","Qty","Size","Material","Description","Length (ft)"
2041,"3","","316L 不锈钢 Sch 10S PE",20
2041,"3","","316L 不锈钢 Sch 10S PE",2.21
5044,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD",
2523,1,"3","Stainless Steel: 316L","316L-SS-SCH10 短半径 90",
2522,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD",
运行在保存的文件上启用宏后,字符串变量(下面代码中的 sTemp)包含 CSV 文件中的正确文本。
我想知道如何消除 CSV 开头的 NUL 字符串,并希望尽可能避免以编程方式打开和保存 CSV 文件。
'Variables for accessing and cleaning text files
Dim oFSO As FileSystemObject 'File System Object grants access to computer files and folders
Dim sSourceFolder As String 'Path of oSourceFolder
Dim oSourceFolder 'Folder the original CSV file is stored in
Dim oFile 'The orignal CSV
Dim sFileToCopy As String 'Path of the copied CSV
Dim sTargetFolder As String 'The path of oTargetFolder
Dim oTargetFolder 'The folder the new CSV file is stored in
Dim oCopiedText 'The copied text file
Dim oCopiedFile 'The copied file itself
Dim oInput 'Represents the text in the CSV ror readin in CSV to memory
Dim sTemp As String 'For storing CSV data in memory
Dim cFiles As New Collection 'Storage for all the files in all the sub folders
Dim sPath As String 'The path of one of the files, will be reused for each file
'variables for progress
Dim iFiles As Integer
Dim Progress As Double
'Constants for reading and writing to text files
Const ForReading = 1
Const ForWriting = 2
'File System Object grants access to computer files and folders
Set oFSO = CreateObject("Scripting.FileSystemObject")
'Select the folder to get CSV files from
sSourceFolder = GetFolder("C:\", "Select Folder Containing CSV files")
Set oSourceFolder = oFSO.GetFolder(sSourceFolder) 'Tell File System Object to get a hold of oSourceFolder so you can look into it
'Check/create the _Cleaned folder inside of the source folder
On Error Resume Next
Err.Clear
sTargetFolder = oSourceFolder.Path & "\" & oSourceFolder.Name & "_Cleaned"
Set oTargetFolder = oFSO.GetFolder(sTargetFolder)
If Err.Number <> 0 Then
'create the folder
oTargetFolder = oFSO.CreateFolder(sTargetFolder)
End If
On Error GoTo 0
'Loop through and clean each CSV so it can be opened in excel properly
iFiles = oSourceFolder.Files.Count
Progress = 0
For Each oFile In oSourceFolder.Files 'go through each file
Application.StatusBar = "Progress: " & Progress & " of " & CStr(iFiles) & ": " & Format(Progress / iFiles, "0%")
If Right(oFile, 3) = "csv" Then 'if it is a text file...
'Copy the original file path
sFileToCopy = oFile.Path
'Open txt file in memory
Set oInput = oFSO.OpenTextFile(sFileToCopy, ForReading)
'Input txt from file to memory
sTemp = oInput.ReadAll
'sTemp contains a repeating SOH string if I do not save the files first and the macro fails downstream
'If I open the CSV file in excel, save it, close it, then run the macro, sTemp contains the string data in the file and the macro runs fine
'此时我可以以某种方式删除 NUL 字符串吗?
.
.
.
我希望 sTemp 值读取 CSV 文件中的实际文本:
"图案、数量、尺寸、Material、描述、长度(英尺)
2041,3,316L 不锈钢 Sch 10S PE,20"
相反,VBA 本地 window 中的 sTemp 值是:
“[[[[[[[[[[[[[[[[[[[[[[”
当我复制该值并将其粘贴到文本编辑器中时,它显示为:
“1
SOH SOH SOH SOH SOH SOH
文件保存在这里:https://www.dropbox.com/sh/e8ohn61bxqidejd/AAB9CMno8N_EXdlA83TBX602a?dl=0。
感谢任何帮助。
你确实有一个前两个字符为 ASCI 0 的文件。
是看到两个主要选项:
- 修复创建这些文件的导出,使其不生成 NULL,或者
- 去除前导 NULL 字符。
对于选项 2,似乎 ReadAll 失败,但 ReadLine 有效。
去除NULL的演示
Sub Demo()
Dim oFSO As FileSystemObject
Dim oInput As TextStream
Dim sTemp
Set oFSO = New FileSystemObject
Set oInput = oFSO.OpenTextFile("Your\File.csv")
sTemp = oInput.ReadLine
Do While Left$(sTemp, 1) = Chr(0)
sTemp = Mid$(sTemp, 2)
Loop
Do Until oInput.AtEndOfStream
sTemp = sTemp & vbCrLf & oInput.ReadLine
Loop
End Sub
我有一个程序可以导出 material 的账单(请参阅 post 中下方的 CSV 示例数据)。
我写了一个 VBA 宏,它使用 FileSystemObject 循环访问用户选择的文件夹,打开 CSV 文件进行读取并使用 ReadAll 将数据读入内存,然后将数据存储并排序 excel.
我遇到的问题是在导出 CSV 文件后,我 运行 CSV 文件中的宏和文本作为几个方括号存储在我的字符串变量(下面代码中的 sTemp)中。我复制了字符串变量的值并将其粘贴到文本编辑器中,它显示为几个包含 "SOH" 的重复黑框。因为数据读取错误导致宏下游失效
如果我打开原始 CSV 文件,前两个字符是黑框,上面写着 "NUL"。 NUL NUL "Pattern","Qty","Size","Material","Description","Length (ft)" 2041,"3","","316L 不锈钢 Sch 10S PE",20 2041,"3","","316L 不锈钢 Sch 10S PE",2.21 5044,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD", 2523,1,"3","Stainless Steel: 316L","316L-SS-SCH10 短半径 90", 2522,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD",
如果我将 CSV 文件保存在 Excel 中并关闭它们,两个 "NUL" 字符就会消失: "Pattern","Qty","Size","Material","Description","Length (ft)" 2041,"3","","316L 不锈钢 Sch 10S PE",20 2041,"3","","316L 不锈钢 Sch 10S PE",2.21 5044,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD", 2523,1,"3","Stainless Steel: 316L","316L-SS-SCH10 短半径 90", 2522,1,"3","Stainless Steel: 304L","DET S10 SS BUTT WELD",
运行在保存的文件上启用宏后,字符串变量(下面代码中的 sTemp)包含 CSV 文件中的正确文本。
我想知道如何消除 CSV 开头的 NUL 字符串,并希望尽可能避免以编程方式打开和保存 CSV 文件。
'Variables for accessing and cleaning text files
Dim oFSO As FileSystemObject 'File System Object grants access to computer files and folders
Dim sSourceFolder As String 'Path of oSourceFolder
Dim oSourceFolder 'Folder the original CSV file is stored in
Dim oFile 'The orignal CSV
Dim sFileToCopy As String 'Path of the copied CSV
Dim sTargetFolder As String 'The path of oTargetFolder
Dim oTargetFolder 'The folder the new CSV file is stored in
Dim oCopiedText 'The copied text file
Dim oCopiedFile 'The copied file itself
Dim oInput 'Represents the text in the CSV ror readin in CSV to memory
Dim sTemp As String 'For storing CSV data in memory
Dim cFiles As New Collection 'Storage for all the files in all the sub folders
Dim sPath As String 'The path of one of the files, will be reused for each file
'variables for progress
Dim iFiles As Integer
Dim Progress As Double
'Constants for reading and writing to text files
Const ForReading = 1
Const ForWriting = 2
'File System Object grants access to computer files and folders
Set oFSO = CreateObject("Scripting.FileSystemObject")
'Select the folder to get CSV files from
sSourceFolder = GetFolder("C:\", "Select Folder Containing CSV files")
Set oSourceFolder = oFSO.GetFolder(sSourceFolder) 'Tell File System Object to get a hold of oSourceFolder so you can look into it
'Check/create the _Cleaned folder inside of the source folder
On Error Resume Next
Err.Clear
sTargetFolder = oSourceFolder.Path & "\" & oSourceFolder.Name & "_Cleaned"
Set oTargetFolder = oFSO.GetFolder(sTargetFolder)
If Err.Number <> 0 Then
'create the folder
oTargetFolder = oFSO.CreateFolder(sTargetFolder)
End If
On Error GoTo 0
'Loop through and clean each CSV so it can be opened in excel properly
iFiles = oSourceFolder.Files.Count
Progress = 0
For Each oFile In oSourceFolder.Files 'go through each file
Application.StatusBar = "Progress: " & Progress & " of " & CStr(iFiles) & ": " & Format(Progress / iFiles, "0%")
If Right(oFile, 3) = "csv" Then 'if it is a text file...
'Copy the original file path
sFileToCopy = oFile.Path
'Open txt file in memory
Set oInput = oFSO.OpenTextFile(sFileToCopy, ForReading)
'Input txt from file to memory
sTemp = oInput.ReadAll
'sTemp contains a repeating SOH string if I do not save the files first and the macro fails downstream
'If I open the CSV file in excel, save it, close it, then run the macro, sTemp contains the string data in the file and the macro runs fine
'此时我可以以某种方式删除 NUL 字符串吗? . . .
我希望 sTemp 值读取 CSV 文件中的实际文本: "图案、数量、尺寸、Material、描述、长度(英尺) 2041,3,316L 不锈钢 Sch 10S PE,20"
相反,VBA 本地 window 中的 sTemp 值是: “[[[[[[[[[[[[[[[[[[[[[[” 当我复制该值并将其粘贴到文本编辑器中时,它显示为: “1 SOH SOH SOH SOH SOH SOH
文件保存在这里:https://www.dropbox.com/sh/e8ohn61bxqidejd/AAB9CMno8N_EXdlA83TBX602a?dl=0。
感谢任何帮助。
你确实有一个前两个字符为 ASCI 0 的文件。
是看到两个主要选项:
- 修复创建这些文件的导出,使其不生成 NULL,或者
- 去除前导 NULL 字符。
对于选项 2,似乎 ReadAll 失败,但 ReadLine 有效。
去除NULL的演示
Sub Demo()
Dim oFSO As FileSystemObject
Dim oInput As TextStream
Dim sTemp
Set oFSO = New FileSystemObject
Set oInput = oFSO.OpenTextFile("Your\File.csv")
sTemp = oInput.ReadLine
Do While Left$(sTemp, 1) = Chr(0)
sTemp = Mid$(sTemp, 2)
Loop
Do Until oInput.AtEndOfStream
sTemp = sTemp & vbCrLf & oInput.ReadLine
Loop
End Sub