代码之家  ›  专栏  ›  技术社区  ›  AmbroseChapel

VBscript有模块吗?我需要处理CSV

  •  1
  • AmbroseChapel  · 技术社区  · 15 年前

    我需要读取CSV文件,唯一可以使用的语言是VBscript。

    我目前正在打开文件并用逗号分割,它工作正常,因为字段中没有任何带引号的逗号。但我知道这是一个非常脆弱的解决方案。

    那么,我可以使用VBscript模块吗?在什么地方可以得到一个经过测试的正则表达式,它只能在逗号上拆分,而不能在引号中拆分?

    4 回复  |  直到 15 年前
        1
  •  7
  •   Tomalak    15 年前

    VBScript没有可与Perl相比的模块系统。但是,您可以使用打开CSV文件 ADO 并像访问数据库表一样访问它们。代码如下所示:

    (这些有趣的评论只是为了修复SO的VB语法错误)

    Dim conn    ''// As ADODB.Connection
    Dim rs      ''// As ADODB.RecordSet
    Dim connStr ''// As String
    Dim dataDir ''// As String
    
    dataDir = "C:\"                         '"
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dataDir & ";Extended Properties=""text"""
    
    Set conn = CreateObject("ADODB.Connection")
    conn.Open(connStr)
    Set rs = conn.Execute("SELECT * FROM [data.txt]")
    
    ''// do something with the recordset
    WScript.Echo rs.Fields.Count & " columns found."
    WScript.Echo "---"
    
    WScript.Echo rs.Fields("Col1Name").Value
    If Not rs.EOF Then
      rs.MoveNext
      WScript.Echo rs.Fields("Col3Name").Value
    End If
    
    ''// explicitly closing stuff is somewhat optional
    ''// in this script, but consider it a good habit
    rs.Close
    conn.Close
    
    Set rs = Nothing
    Set conn = Nothing
    

    创建一个 schema.ini 准确描述您的输入的文件是最佳的。如果您不这样做,您将强制文本驱动程序猜测,如果它猜错了东西,那么所有赌注都将被取消。这个 schema.ini 必须位于数据所在的同一目录中。

    [data.txt]
    Format=Delimited(;)
    DecimalSymbol=.
    ColNameHeader=True
    MaxScanRows=0
    Col1=Col1Name Long
    Col2=Col2Name Long
    Col3=Col3Name Text
    Col4=Col4Name Text
    

    用这个 data.txt :

    a;b;c;d
    1;2;"foo bar";"yadayada"
    1;2;"sample data";"blah"
    

    C:\>cscript -nologo data.vbs
    4 columns found.
    ---
    1
    sample data
    
    C:\>
    

    在这方面值得一读: Much ADO About Text Files 脱离MSDN。

        2
  •  0
  •   daanish.rumani    15 年前

    您可以尝试创建一个Excel ODBC数据源到CSV(我想称为DSN)。它在 控制面板->管理工具->ODBC数据源

        3
  •  0
  •   GSerg    15 年前

    正则表达式:

    'Credits go to http://www.codeguru.com/cpp/cpp/algorithms/strings/article.php/c8153/
    r.Pattern = ",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
    

    或者,您可以使用我刚刚为vbs改编的这个函数。

    call test
    
    
    Function ParseCSV(StringToParse, Quotes)
      Dim i, r(), QuotedItemStart, prevpos
    
      ReDim r(0)
      prevpos = 1
    
      For i = 1 To Len(StringToParse)
        If Mid(StringToParse, i, 1) = "," Then
          If QuotedItemStart = 0 Then
            r(UBound(r)) = Trim(Mid(StringToParse, prevpos, i - prevpos))
            ReDim Preserve r(UBound(r) + 1)
            prevpos = i + 1
          End If
        Else
          If InStr(1, Quotes, Mid(StringToParse, i, 1)) Then
            If QuotedItemStart Then
              r(UBound(r)) = Trim(Mid(StringToParse, QuotedItemStart, i - QuotedItemStart))
              ReDim Preserve r(UBound(r) + 1)
              QuotedItemStart = 0
              prevpos = i + 2
              i = i + 1
            Else
              QuotedItemStart = i + 1
            End If
          End If
        End If
      Next
    
      If prevpos < Len(StringToParse) Then r(UBound(r)) = Trim(Mid(StringToParse, prevpos))
      ParseCSV = r
    End Function
    
    
    Sub Test()
      Dim i, s
    
      s = ParseCSV("""This is, some text!"",25,""Holy holes!"", 286", """")
    
      For i = LBound(s) To UBound(s)
        msgbox s(i)
      Next
    
      msgbox "Items: " & CStr(UBound(s) - LBound(s) + 1)
    End Sub
    
        4
  •  0
  •   Benjol    15 年前

    link to MSDN . 不是纯粹的VBS,但如果这是真正的限制,它应该在“just”窗口中工作。

    更多链接: