有沒有人有VB腳本中的短腳本轉置矩陣(作爲CSV(逗號分隔值)文件)?VBscript - 轉置CSV文件
A, 1, 2, 3
B, 7, 5, 6
- >
A, B
1, 7
2, 5
3, 6
提前非常感謝 湯姆
有沒有人有VB腳本中的短腳本轉置矩陣(作爲CSV(逗號分隔值)文件)?VBscript - 轉置CSV文件
A, 1, 2, 3
B, 7, 5, 6
- >
A, B
1, 7
2, 5
3, 6
提前非常感謝 湯姆
因此,通過建立動態數組,並自動遞增其與發現的新列的增長並行原始矩陣,您可以快速自動構建新的數據結構。
Const OutputCSV = "C:\op.csv"
Dim dt_start, WriteOutput : dt_start = Now
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim file : Set file = fso.OpenTextFile("C:\test.csv", 1, True)
Set WriteOutput = fso.OpenTextFile(OutputCSV, 8, True)
Dim fc : fc = file.ReadAll : file.close : Dim fcArray : fcArray = Split(fc, vbCrLf)
WScript.echo "Before Transpose"
WScript.echo "----------------"
WScript.echo fc
WScript.echo "----------------"
Dim opArray() : ReDim opArray(0)
For Each row In fcArray
Dim tmp: tmp = Split(row, ",")
For ent=0 To UBound(tmp)
If ent > UBound(opArray) Then
ReDim Preserve opArray(UBound(opArray)+1)
opArray(ent) = Trim(tmp(ent))
Else
If Len(opArray(ent)) > 0 Then
opArray(ent) = opArray(ent) & "," & Trim(tmp(ent))
Else
opArray(ent) = Trim(tmp(ent))
End If
End If
Next
Next
Dim dt_end : dt_end = Now
WScript.echo "After Transpose"
WScript.echo "----------------"
WScript.echo Join(opArray, vbCrLf)
WScript.echo "----------------"
WScript.echo "Script Execution Time (sec): " & DateDiff("s", dt_start, dt_end)
WriteOutput.Write Join(opArray, vbCrLf) : WriteOutput.Close
如果只是兩行數目相等的值,你可以讀取到使用Split
功能陣列:
a1 = Split(FileIn.ReadLine, ",")
a2 = Split(FileIn.ReadLine, ",")
然後,迭代數組和令狀e每個元素:
For i = 0 To UBound(a1)
FileOut.WriteLine a1(i) & ", " & a2(i)
Next
我假設你知道如何打開文件進行讀寫?
編輯:這聽起來像你可能有未知數量的行要閱讀。在這種情況下,你可以使用一個數組的數組:
Dim a(255) ' Hold up to 255 rows. Adjust as needed. Or use ReDim Preserve to grow dynamically.
Do Until FileIn.AtEndOfStream
a(i) = Split(FileIn.ReadLine, ",")
i = i + 1
Loop
然後,寫:
For j = 0 To UBound(a(0))
' Concatenate the elements into a single string...
s = ""
For k = 0 To i - 1
s = s & a(k)(j) & ","
Next
' Write the string without the final comma...
FileOut.WriteLine Left(s, Len(s) - 1)
Next
沒有遺憾,矩陣可以任意大。儘管如此,謝謝你的幫助。 –
哇!做得好!它完美的作品。非常感謝 –