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

删除CSV中不必要的数据/空间

vb6
  •  0
  • Elmar  · 技术社区  · 7 年前

    12345,"        ","abcde fgh",2017-06-06,09:00,AM,"       ", US
    

    12345,,"abcde fgh",2017-06-06,09:00,AM,, US
    

    自从 " " 应视为空。

    这是我的示例函数。

    Private Sub Transform(delimiter As String)
    
       Dim sFullPath    As String
       Dim strBuff      As String
       Dim re           As RegExp
       Dim matches      As Object
       Dim m            As Variant
    
       If delimiter <> "," Then
          strBuff = Replace(strBuff, delimiter, ",")
       Else
            With re
                .Pattern = "(?!\B""[^""]*)" & delimiter & "(?![^""]*""\B)"
                .IgnoreCase = False
                .Global = True
            End With
    
            Set matches = re.Execute(strBuff)
            For Each m In matches
                strBuff = re.Replace(strBuff, ",")
            Next
    
            Set re = Nothing
            Set matches = Nothing  
        End If
    End Sub
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   jac    7 年前

    Chr(34) 在字符串中包含双引号。

    \B(\s)(?!(?:[^""]*""[^""]*"")*[^""]*$)

    12345," ","abcde fgh",2017-06-06,09:00,AM," ", US

    产量

    12345,"","abcde fgh",2017-06-06,09:00,AM,"", US

    示例函数

    Private Function Transform(ByVal strLine As String) As String
        Dim objRegEx As RegExp
    
        On Error GoTo ErrTransForm
    
        Set objRegEx = New RegExp
        With objRegEx
            .Pattern = "\B(\s)(?!(?:[^""]*""[^""]*"")*[^""]*$)"
            .IgnoreCase = False
            .Global = True
            Transform = .Replace(strLine, "")
        End With
    
    ExitTransForm:
        If Not objRegEx Is Nothing Then
            Set objRegEx = Nothing
        End If
        Exit Function
    
    ErrTransForm:
        'error handling code here
        GoTo ExitTransForm
    
    End Function
    

    信用是应得的。我用这个答案, Regex Replace Whitespaces between single quotes

        2
  •  0
  •   Bugs Manjeet    7 年前

    我会添加一个输出字符串变量,并有一个条件语句,表示如果输入不是空的,请将其添加到输出字符串中。例如(VB控制台应用程序格式,提示用户输入许多输入):

    Dim input As String
    Dim output As String
    
    Do
        input = console.ReadLine()
        If Not input = " " Then
            output += input
        End If
    Loop Until (end condition)
    
    Console.WriteLine(output)
    

    您可以将不希望的任何输入抛出到条件中,以将其从输出中删除。

        3
  •  -3
  •   Giorgio Brausi    7 年前

    您的CSV文件格式不正确。

    在这之后,你现在有了一个真正的CSV文件,你可以导入没有问题。