简体   繁体   English

使用 VBScript 将 CSV 列数据拆分为新的 CSV 文件

[英]Splitting CSV column data into new CSV file using VBScript

I have a CSV file where 2 columns contain several different text values eg我有一个 CSV 文件,其中 2 列包含几个不同的文本值,例如

Column 1: Reptiles, Health, Hygiene  
Column 2: Purity

I need to use VBscript to split these columns into a new CSV file without changing the current file, expected output in new CSV file shown below:我需要使用 VBscript 将这些列拆分为一个新的 CSV 文件而不更改当前文件,新 CSV 文件中的预期输出如下所示:

Column 1    Column 2
Reptiles        Reptiles
Health          Health
Hygiene         Hygiene
Purity          Purity

Unfortunately(?) it must be done with VB Script and nothing else.不幸的是(?)它必须用 VB Script 来完成,没有别的。

Here is an example of how the data looks (of course the data consistently repeats with some extra entries through the same columns in file 1.这是数据外观的示例(当然,数据始终通过文件 1 中的相同列重复并带有一些额外条目。

原始文件

And here is an example of how it needs to look but it needs to repeat down until all unique entries from Column 1 and 2 in the original file have been input as a single entry to Column 1 in the new file and copied to Column 2 in the same new file.这是它需要如何查看的示例,但它需要重复直到原始文件中第 1 列和第 2 列的所有唯一条目作为单个条目输入到新文件中的第 1 列并复制到第 2 列同一个新文件。 eg例如

新文件

Examples in text format as requested:按要求提供的文本格式示例:

Original file:原始文件:

Column 1,Column 2
"Reptiles, Health, Hygiene",Purity

New File:新文件:

Column 1,Column 2
Reptiles,Reptiles
Health,Health
Hygiene,Hygiene
Purity,Purity

I think this is a simple matter of using the FileSystemObject with Split function.我认为这是将FileSystemObjectSplit函数一起使用的简单问题。 Assuming each input line is just one set of data you can remove the double quotes and process from there假设每个输入行只是一组数据,您可以从那里删除双引号和处理

Try this VB script out ( edited to process header line separately ):试试这个 VB 脚本(编辑为单独处理标题行):

Const Overwrite = True
Set ObjFso = CreateObject("Scripting.FileSystemObject")

Set ObjOutFile = ObjFso.CreateTextFile("My New File Path", Overwrite)
Set ObjInFile = ObjFso.OpenTextFile("My Old File Path")

' Skip processing first header line and just write it out as is
strLine = ObjInFile.ReadLine
ObjOutFile.WriteLine strLine

Do Until ObjInFile.AtEndOfStream
    ' Remove all double quotes to treat this as one set of data
    strLine = Replace(ObjInFile.ReadLine, """","")
    varData = Split(strLine,",")
    ' Write out each element twice into its own line
    For i = 0 to uBound(varData)
        ObjOutFile.WriteLine varData(i) & "," & varData(i) 
    Next i
Loop

ObjInFile.Close
ObjOutFile.Close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM