Jump to content

csv to 2D array with regexp


Recommended Posts

There are many exemples with StringRegExp but deals only about getting 1 Dim Array as result.

I don't know if it is a limitation of regexp, but if not, would you please tell me how to parse csv fields with regexp ? the result must be a 2 Dim array.

here is the csv string to parse:

"010","2","03"

"24","30","20"

"txt","bla","toto"

my code is:

My_2Aarray=stringregexp($csv,'(".*?"),(".*?"),(".*?")',3)

still give me 1 Dim Array :D

Link to comment
Share on other sites

I don't think RegExp does anything 2D like that. You'll have to essentially StringSplit() each line of the file in a loop, and then do a 2D version of _ArrayConcatenate() onto the results array.

:D

PS: Here's a neat idea I just thought of, but haven't tried: Open the .csv as a database via Excel (I think ptrex posted an example of that), then use the ADO .getRows function to get the whole thing as a 2D array in one operation. It might even work!

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Here is a function that converts CSV format to a 2D array.

;
#include <array.au3>

Opt("MustDeclareVars", 1)

Dim $sCSV = '"010","2","03"' & @CRLF & _
        '"24","30","20"' & @CRLF & _
        '"txt","bla","toto"'

Local $arr = _CsvToArray2D($sCSV)
_ArrayDisplay($arr)

; Converts CSV format to a 2D array.
Func _CsvToArray2D($sCSV)
    Local $aTmp = StringRegExp($sCSV & @CR, '(\V*)\v{1,2}', 3)
    Local $NumCols[UBound($aTmp)]
    For $x = 0 To UBound($aTmp) - 1
        StringReplace($aTmp[$x], ",", ",")
        $NumCols[$x] = @extended + 1
    Next
    Local $Max = _ArrayMax($NumCols, 1)

    Dim $aArr[UBound($aTmp)][$Max]

    For $i = 0 To UBound($aArr, 1) - 1
        Local $aTemp = StringSplit($aTmp[$i], ",")
        For $j = 0 To $aTemp[0] - 1
            $aArr[$i][$j] = $aTemp[$j + 1]
        Next
    Next
    Return $aArr
EndFunc  ;==>_CsvToArray2D
;========> End of _CsvToArray2D ======================
;

Using PsaltyDS's Excel idea, I have found _ExcelReadSheetToArray () works well once you get your head around what the parameters of the function do.

Its reverse function, _ExcelWriteSheetFromArray(), should you need it, is also recommended. Both functions are in the help file with examples.

Edit: Replaced this:-

Local $sTmp = StringReplace($sCSV, @CR, @LF)

$sTmp = StringReplace($sTmp, @LF & @LF, @LF)

Local $aTmp = StringSplit($sTmp, @LF)

Local $NumRows = $aTmp[0]

_ArrayDelete($aTmp, 0)

Local $NumCols[$NumRows]

with this:-

Local $aTmp = StringRegExp($sCSV & @CR, '(\V*)\v{1,2}', 3)

Local $NumCols[uBound($aTmp)]

Edited by Malkey
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...