Validate a csv file

前端 未结 6 500
暖寄归人
暖寄归人 2021-01-20 02:34

This is my sample file

#%cty_id1,#%ccy_id2,#%cty_src,#%cty_cd3,#%cty_nm4,#%cty_reg5,#%cty_natnl6,#%cty_bus7,#%cty_data8
690,ALL2,,AL,ALBALODMNIA,,,,
90,ALL2,,         


        
6条回答
  •  醉话见心
    2021-01-20 02:46

    Coming to the party late with a VBA based approach. An alternative way to regex is to to parse the file and remove a comma when the 4th field is empty. Using microsoft scripting runtime this can be acheived the code opens a the file then reads each line, copying it to a new temporary file. If the 4 element is empty, if it is it writes a line with the extra comma removed. The cleaned data is then copied to the origonal file and the temporary file is deleted. It seems a bit of a long way round, but it when I tested it on a file of 14000 rows based on your sample it took under 2 seconds to complete.

    Sub Remove4thFieldIfEmpty()
    
        Const iNUMBER_OF_FIELDS As Integer = 9
    
        Dim str As String
        Dim fileHandleInput As Scripting.TextStream
        Dim fileHandleCleaned As Scripting.TextStream
        Dim fsoObject As Scripting.FileSystemObject
        Dim sPath As String
        Dim sFilenameCleaned As String
        Dim sFilenameInput As String
        Dim vFields As Variant
        Dim iCounter As Integer
        Dim sNewString As String
    
        sFilenameInput = "Regex.CSV"
        sFilenameCleaned = "Cleaned.CSV"
        Set fsoObject = New FileSystemObject
    
        sPath = ThisWorkbook.Path & "\"
    
    
        Set fileHandleInput = fsoObject.OpenTextFile(sPath & sFilenameInput)
    
        If fsoObject.FileExists(sPath & sFilenameCleaned) Then
            Set fileHandleCleaned = fsoObject.OpenTextFile(sPath & sFilenameCleaned, ForWriting)
        Else
            Set fileHandleCleaned = fsoObject.CreateTextFile((sPath & sFilenameCleaned), True)
        End If
    
    
        Do While Not fileHandleInput.AtEndOfStream
            str = fileHandleInput.ReadLine
                vFields = Split(str, ",")
                If vFields(3) = "" Then
                    sNewString = vFields(0)
                    For iCounter = 1 To UBound(vFields) 
                        If iCounter <> 3 Then sNewString = sNewString & "," & vFields(iCounter)
                    Next iCounter
                    str = sNewString
                End If
            fileHandleCleaned.WriteLine (str)
        Loop
    
    
        fileHandleInput.Close
        fileHandleCleaned.Close
    
        Set fileHandleInput = fsoObject.OpenTextFile(sPath & sFilenameInput, ForWriting)
        Set fileHandleCleaned = fsoObject.OpenTextFile(sPath & sFilenameCleaned)
    
        Do While Not fileHandleCleaned.AtEndOfStream
            fileHandleInput.WriteLine (fileHandleCleaned.ReadLine)
        Loop
    
        fileHandleInput.Close
        fileHandleCleaned.Close
    
    
    
        Set fileHandleCleaned = Nothing
        Set fileHandleInput = Nothing
    
        KillFile (sPath & sFilenameCleaned)
    
        Set fsoObject = Nothing
    
    
    End Sub
    

提交回复
热议问题