我的txt文件中有以下格式:
1 1,30705856804525E-7 2,64163961816166E-8 1,12201845645905 1,24157281788939E-7 2,45690063849224E-8 1,25892543792725 1,18248813407718E-7 2,29960868125545E-8 1,41253757476807 1,13006606738963E-7 2,16654658657944E-8 1,58489322662354 1,0842624220686E-7 2,05472137082552E-8 1,77827942371368 1,04479198625995E-7 1,96135836461053E-8 1,99526226520538 1,01119816520168E-7 1,8839035220708E-8 2,23872113227844 9,82917924829962E-8 1,82003176973922E-8 2,51188635826111 9,59338279926669E-8 1,76765304615856E-8 2,81838297843933 9,39840489877497E-8 1,72491425587395E-8 3,16227769851685 9,23831819932275E-8 1,69019571671924E-8 3,54813385009766 9,10766573269939E-8 1,66210121221866E-8 3,98107171058655 9,00157104410937E-8 1,63944182673958E-8 4,46683597564697 8,91577514039454E-8 1,62121711611007E-8 5,01187229156494 8,8466336478632E-8 1,60659361370108E-8 5,6234130859375 8,7910699164695E-8 1,59488209305891E-8 6,30957365036011 8,74651959748007E-8 1,58551749507296E-8 7,07945775985718 8,71086527354237E-8 1,57803938805046E-8 7,94328212738037 8,68237393092386E-8 1,57207402651238E-8 8,91250896453857 8,65963372120859E-8 1,56731942979604E-8 10 8,64150138113473E-8 1,56353241465013E-8 11,2201843261719 8,62705391568852E-8 1,5605175818223E-8
我需要只获取左右值的整数值,所以在这个例子中我需要得到:
1 2,64163961816166E-8 10 1,56353241465013E-8
这是我尝试过的:
' Check Noise Spectral Density.txt exists
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(fso.GetParentFolderName(WScript.ScriptFullName) + "\Projects\Noise Spectral Density.txt")) Then
' Open the file for input.
Set NoiseSpectralDensity = fso.OpenTextFile(fso.GetParentFolderName(WScript.ScriptFullName) + "\Projects\Noise Spectral Density.txt", 1)
' Noise Variables
Dim Noise
' Read from the file and display the results.
Do While NoiseSpectralDensity.AtEndOfStream <> True
' Read Line By Line
TextLine = NoiseSpectralDensity.ReadLine
' Check If Number
'If (IsNumeric(Left(TextLine, 5))) Then
' Get Noise
' Noise @ 1kHz
Noise = Right(TextLine, InStr(Mid(TextLine, 2), InStr(TextLine, " ")-1))
x = MsgBox("SNR: " & Split(Left(TextLine, 5), " ")(0) & " NOISE: " & Noise & "",0, "Noise Spectral Density")
'End If
Loop
' Close the file for input.
NoiseSpectralDensity.Close
Else
x = MsgBox("Noise Spectral Density.txt NOT Found!" & vbCrLf & "Wave->Save As Text...", 0, "Noise Spectral Density")
End If
但我无法使用Split(TextLine, " ")(0)
在VBScript中获得左右数字。
答案 0 :(得分:2)
您的数据似乎是以制表符分隔的,因此您可以执行以下操作:
arr = Split(TextLine, vbTab)
If Not (InStr(arr(0), ",") > 0) Then
'first number doesn't have decimals
snr = arr(0)
noise = arr(2)
End If
答案 1 :(得分:1)
虽然@AnsgarWiechers提供的解决方案应该有效,但是如果它没有,你可以使用正则表达式(用以下代替整个Do-while循环):
Set objReg = New RegExp
objReg.Pattern = "^(\d+)(?=\s).*\s+([\d,Ee-]+)$" 'See the explanation below
Do While NoiseSpectralDensity.AtEndOfStream <> True
'Read Line By Line
TextLine = NoiseSpectralDensity.ReadLine
' Check If Number
Set objMatches = objReg.Execute(TextLine)
For Each objMatch In objMatches
SNR = objMatch.submatches.item(0)
Noise = objMatch.submatches.item(1)
MsgBox "SNR: "&SNR&"; Noise: "&Noise
Next
Loop
<强> Click for Regex Demo 强>
正则表达式说明:
^
- 断言字符串的开头(\d+)
- 匹配1个以上的数字并在第1组中捕获它(?=\s)
- 积极前瞻以找到紧靠白色空间的位置。因此,步骤2中的数字将匹配,直到遇到空格(空格,制表符等).*
- 匹配除换行符之外的任何字符的0 +次出现\s+
- 匹配1个以上的空格([\d,Ee-]+)
- 匹配1个以上的数字或,
或-
或字母E
或e
,并将其捕获到第2组$
- 断言字符串的结尾