如何使用VBScript从CSV读取引用字段

时间:2014-08-20 19:49:36

标签: parsing csv vbscript

在具有固定列数的sample.csv文件中,我必须提取特定字段值并使用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"

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

4 个答案:

答案 0 :(得分:2)

您可以尝试使用ADO连接

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个字符,则可能会出现问题 - 如果是,则可能会返回截断的字段。您也可以使用ODBC或ACE连接字符串,而不是我在此处使用过的Jet字符串。

答案 1 :(得分:1)

由于CSV以逗号分隔,因此您可以使用Split()函数将字段分隔为数组:

' Read a line from the CSV...
strLine = myCSV.ReadLine()

' Split by comma into an array...
a = Split(strLine, ",")

由于您有静态列数(5),因此最后一个字段将始终为a(4),而倒数第二个字段将为a(3)

答案 2 :(得分:0)

您的CSV数据似乎每行包含2个嵌入式硬回车(CR,LF)。然后第一行ReadLine返回:

  

100,SN,100.SN," 100 | SN | 435623 | serkasg | 15.32 |

下面的解决方案在提取必填字段之前解开这些行。

Option Explicit

Const ForReading = 1
Const ForAppending = 8

Const TristateUseDefault = 2 ' Opens the file using the system default. 
Const TristateTrue = 1       ' Opens the file as Unicode. 
Const TristateFalse = 0      ' Opens the file as ASCII. 

Dim FSO, TextStream, Line, LineNo, Fields, Field4, Field5

ExtractFields "sample.csv"

Sub ExtractFields(FileName)
  Set FSO = CreateObject("Scripting.FileSystemObject")
  If FSO.FileExists(FileName) Then
    Line = ""
    LineNo = 0
    Set TextStream = FSO.OpenTextFile(FileName, ForReading, False, TristateFalse)
    Do While Not TextStream.AtEndOfStream
      Line = Line & TextStream.ReadLine()
      LineNo = LineNo + 1
      If LineNo mod 3 = 0 Then
        Fields = Split(Line, ",")
        Field4 = Fields(3)
        Field5 = Fields(4)
        MsgBox "Line " & LineNo / 3 & ": " & vbNewLine & vbNewLine _
          & "Field4: " & Field4 & vbNewLine & vbNewLine _
          & "Field5: " & Field5

        Line = ""
      End If
    Loop
    TextStream.Close()
  Else
    MsgBox "File " & FileName & " ... Not found"
  End If
End Sub

答案 3 :(得分:0)

这是一种允许单行或多行CSV记录的替代解决方案。它使用正则表达式,简化了处理多行记录的逻辑。此解决方案不会删除记录中嵌入的CRLF字符;我已经把它作为锻炼给你了:))

Option Explicit

Const ForReading = 1
Const ForAppending = 8

Const TristateUseDefault = 2 ' Opens the file using the system default. 
Const TristateTrue = 1       ' Opens the file as Unicode. 
Const TristateFalse = 0      ' Opens the file as ASCII. 

Dim FSO, TextStream, Text, MyRegExp, MyMatches, MyMatch, Field4, Field5

ExtractFields "sample.csv"

Sub ExtractFields(FileName)
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(FileName) Then
        Set MyRegExp = New RegExp
        MyRegExp.Multiline = True
        MyRegExp.Global = True
        MyRegExp.Pattern = """([^""]+)"",""([^""]+)"""
        Set TextStream = FSO.OpenTextFile(FileName, ForReading, False, TristateFalse)
        Text = TextStream.ReadAll
        Set MyMatches = MyRegExp.Execute(Text)
        For Each MyMatch in MyMatches
            Field4 = SubMatches(0)
            Field5 = SubMatches(1)
            MsgBox "Field4: " & vbNewLine & Field4 & vbNewLine & vbNewLine _
                & "Field5: " & vbNewLine & Field5, 0, "Found Match"
        Next
        Set MyMatches = Nothing
        TextStream.Close()
    Else
        MsgBox "File " & FileName & " ... Not found"
    End If
End Sub