如何使用VBScript从CSV中读取引用的字段 [英] How to read quoted field from CSV using VBScript

查看:642
本文介绍了如何使用VBScript从CSV中读取引用的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sample.csv文件中,它有固定的列数,我必须提取一个特定的字段值,并使用VBScript将它存储在一个变量中。

In a sample.csv file, which has fixed number of columns, I have to extract a particular field value and store it in a variable using VBScript.

sample.csv

100,SN,100.SN,"100|SN|   435623|   serkasg|  15.32|
               100|SN|   435624|   serkasg|  15.353|
               100|SN|   437825|   serkasg|  15.353|"," 0 2345"
101,SN,100.SN,"100|SN|   435623|   serkasg|  15.32|
               100|SN|   435624|   serkasg|  15.353|
               100|SN|   437825|   serkasg|  15.353|"," 0 2346"

我要解析在双引号内的最后两个字段,并将它们存储在每一行的两个不同的数组变量中。

I want to parse the last two fields which are within double quotes and store them in two different array variables for each row.

推荐答案

您可以尝试使用ADO连接

You could try using an ADO connection

Option Explicit

dim ado: set ado = CreateObject("ADODB.Connection")
ado.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtFilesFolder\;Extended Properties=""text;HDR=No;FMT=Delimited"";"
ado.open

dim recordSet: set recordSet = ado.Execute("SELECT * FROM [samples.csv]")

dim field3, field4

do until recordSet.EOF

    field3 = recordSet.Fields(3).Value
    field4 = recordSet.Fields(4).Value

    ' use your fields here

    recordSet.MoveNext
loop
recordSet.close
ado.close

如果这些字段的长度大于255个字符,您可能会遇到问题 - 如果字段长度超过255个字符,那么它们可能会返回截断。您也可以更好地运行 ODBC 或ACE连接字符串,而不是Jet一个我在这里使用。

You may have an issue if those fields are greater than 255 characters in length - if they are, they may return truncated. You also may have better luck with ODBC or ACE connection strings instead of the Jet one I've used here.

这篇关于如何使用VBScript从CSV中读取引用的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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