VBscript - 转置CSV文件 [英] VBscript - Transpose CSV File

查看:267
本文介绍了VBscript - 转置CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人在VBscript中有一个简短的脚本用于转置矩阵(以CSV(逗号分隔值)文件形式给出)?

Does anyone have a short script in VBscript for transposing a Matrix (given as CSV (comma separated values) file)?

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

- >

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

很感谢提前
Tom

Many Thanks in advance Tom

推荐答案

因此,通过创建动态数组并自动增长它们的增长,同时发现原始矩阵的新列,可以快速自动构建新的数据结构。

So by creating dynamic arrays and auto-increment their growth in parallel with discovering new columns of the original matrix, you can auto build the new data structure quite quickly.

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

这篇关于VBscript - 转置CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆