2014-03-25 2 views
0

VBscript에서 매트릭스를 전치시키기위한 짧은 스크립트 (CSV (쉼표로 구분 된 값) 파일로 제공)가있는 사람이 있습니까?VBscript - Transpose CSV File

A, 1, 2, 3 
B, 7, 5, 6 

->

A, B 
1, 7 
2, 5 
3, 6 
사전에

많은 감사 톰

답변

0

를 따라서 동적 배열 및 자동 증가의 새 열을 발견 병렬로 성장을 만들어 원래의 매트릭스를 사용하면 새로운 데이터 구조를 매우 빠르게 자동 빌드 할 수 있습니다.

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 
+0

와우! 훌륭한 일! 그것은 완벽하게 작동합니다. 많은, 많은 감사 –

1

이 값의 동일한 수의 두 라인의 경우, Split 기능을 사용하여 배열로 모두 읽을 수 있습니다

a1 = Split(FileIn.ReadLine, ",") 
a2 = Split(FileIn.ReadLine, ",") 

그런 다음 배열을 반복하여 기록하십시오. 각 요소 :

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 
+0

더 미안

Matrix Transposer

, 매트릭스는 임의의 큰 수 있습니다. 그럼에도 불구하고 도움 주셔서 감사합니다. –