实际上,我正在编写一个文件转换工具。数据值表示120x120矩阵。我的第一个想法是两个嵌套的for循环,在第一个循环中只有一个新的行:简单。
输入文件具有每个fith值的vbNewLine。我将输入文件解析为字符串strAll。
输入
strAll包含10个字符长值
" -24.1189" (two blanks before value)
" 1.2345" (four blanks before value)输出
-24.1189;-24.1189; (...total 120 values...)
-24.1234;-24.1189; (...total 120 values...)
(... total 120 rows ...)使用Mid hat应该很容易解析:Mid(strAll, 1+i, 10+i),其中i是for循环中的计数器。一个Replace(stAll, " ", "")应该删除所有的3个空白,2个空白和一个空白。
问题:如何将字符串输出到格式化为矩阵的文件中?
Dim intValueLength, maxValue, intValueInRowMax
intValueLength=10
intValueInRowMax=120
maxValues=intValueInRowMax * intValueInRowMax
Sub Strg2Array
arrAll = array()
' convert to array
For i=1 To maxValues
ReDim Preserve arrAll(UBound(arrAll) +1)
arrAll(UBound(arrAll)) = Mid(strAll, 1+(i-1)*intValueLength, i*intValueLength)
Next
End Sub
Sub SaveAll
Dim intValueInRow
intValueInRow=0
Const ForWriting = 2
Set objFSOOut = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSOOut.OpenTextFile(strFileName, ForWriting, true)
for each value in arrAll
objOutput.Write value
intValueInRow = intValueInRow + 1 'Argh, there is no "++" operator?
If (intValueInRow = intValueInRowMax) Then
objOutput.Write vbNewLine
intValueInRow=0
End If
next
objOutput.Close
Set objFSOOutput = Nothing
End Sub发布于 2012-10-16 17:39:59
(几乎)与Ansgar相同的想法,不同的实现:
Const cnRows = 4 ' to keep the demo simple
Const cnCols = 3
Const csFSep = ";"
' Const csRSep = vbLf
Dim csRSep : csRSep = vbLf
Dim aInps : aInps = Array( _
Join(Array("A1 B1 C1 A2", vbLf, "B2,C2", "comment", vbCrLf, "A3 B3 C3", vbTab, "A4-B4-C4")) _
, "1 2 3 4 5 6 7 8 9 10 11 12" _
, Join(Array("1.1 -2.2 3.3 4", vbLf, "0.5 -6.6", "comment", vbCrLf, "7 8.0 -9.99", vbTab, "10.10*11.11***-12.12")) _
)
' no need to care for the input format, as long as the valid data can be
' specified by a RegExp pattern (and the file contains 'enough' info)
Dim reCut : Set reCut = New RegExp
reCut.Global = True
reCut.Pattern = "([A-Z-])?\d+(\.\d+)?"
Dim sInp
For Each sInp In aInps
WScript.Echo Join(Array("----", vbCrLf, sInp, vbCrLf, "----"), "")
Dim oMTS : Set oMTS = reCut.Execute(sInp)
If oMTS.Count <> cnRows * cnCols Then
WScript.Echo "Bingo:", oMTS.Count, "<>", (cnRows * cnCols)
Else
Dim m
For m = 0 To oMTS.Count - 1
WScript.Stdout.Write oMTS(m).Value
If 0 = (m + 1) Mod cnCols Then WScript.Stdout.Write csRSep Else WScript.Stdout.Write csFSep
Next
End If
Next产出:
----
A1 B1 C1 A2
B2,C2 comment
A3 B3 C3 A4-B4-C4
----
A1;B1;C1
A2;B2;C2
A3;B3;C3
A4;B4;C4
----
1 2 3 4 5 6 7 8 9 10 11 12
----
1;2;3
4;5;6
7;8;9
10;11;12
----
1.1 -2.2 3.3 4
0.5 -6.6 comment
7 8.0 -9.99 10.10*11.11***-12.12
----
1.1;-2.2;3.3
4;0.5;-6.6
7;8.0;-9.99
10.10;11.11;-12.12我的模式试图识别有效数据并避免(可能代价高昂)字符串操作。
发布于 2012-10-16 17:08:27
首先,您的过程Strg2Array()中似乎有一个错误。
arrAlles(UBound(arrAll)) = ...很可能是
arrAll(UBound(arrAll)) = ...话虽如此,我可否建议另一种方法呢?因为您似乎有这样一个输入文件:
" -24.1189";" 1.2345";" 124.5290";" -5.3951";" 2.1062"
" 2.6702";" -23.1502";" -1.5028";" -2.6223";" -24.3573"
...并且似乎希望用数字(没有双引号和空格)创建分号分隔的输出,如下所示:
-24.1189;1.2345;124.5290;-5.3951;2.1062;...(115 more)...
...我会用更像这样的东西:
Set fso = CreateObject("Scripting.FileSystemObject")
strAll = fso.OpenTextFile(WScript.Arguments.Unnamed(0), 1).ReadAll
Set re = New RegExp
re.Pattern = "\s+"
strAll = Replace(strAll, vbNewLine, ";")
strAll = Replace(strAll, """", "")
strAll = re.Replace(strAll, "")
arrAll = Split(strAll, ";")
For i = 0 To UBound(arrAll)
WScript.StdOut.Write arrAll(i)
If i Mod 120 = 119 Then
WScript.StdOut.WriteLine
Else
WScript.StdOut.Write ";"
End If
Next它将读取作为脚本的第一个参数的文件中的输入,删除双引号和空格,然后将字段写入StdOut (每行120个字段)。将输出重定向到一个新文件,您就完成了。
如果需要用空字段填充最后一行,请追加如下内容:
If (i Mod 120) <> 0 Then
For n = i Mod 120 To 118 ' max. index - 1
WScript.StdOut.Write ";"
Next
End If你可以这样称呼剧本:
cscript.exe input.csv > output.csvhttps://stackoverflow.com/questions/12918152
复制相似问题