.csv-Datei in Array

  • Hallo,

    ich wollte gerne eine CSV-Datei in ein Array einlesen.
    Dazu habe ich dies hier gefunden:

    Spoiler anzeigen
    [autoit]

    #include <array.au3>
    Main()

    [/autoit] [autoit][/autoit] [autoit]

    Func Main()
    Local $sAdr = "C:\\test.csv"
    Local $h = FileOpen($sAdr, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $aRet = _CSV2Array($h, Default, True, 1)

    [/autoit] [autoit][/autoit] [autoit]

    FileClose($h)
    _ArrayDisplay($aRet)

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>Main

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ===================================================================
    ; Name ..........: _CSV2Array
    ; Description ...:
    ; AutoIt Version : V3.3.0.0
    ; Syntax ........: _CSV2Array($hFile[, $cSeperator = "auto"[, $bFilterString = True[, $iColumnMode = 0]]])
    ; Parameter(s): .: $hFile - Handle for the CSV file to Read
    ; $cSeperator - Optional: (Default = "auto") : Tries to find the separator char ;) or , or TAB or | or space)
    ; | Data-seperator-char
    ; | Empty-string = Opt("GUIDataSeparatorChar")
    ; $bFilterString - Optional: (Default = True) : Removes leading and trailing " or '
    ; $iColumnMode - Optional: (Default = 0) :
    ; | 0: Sets error if lines have different columns and @extended to the csv-line number
    ; | 1: returns lines with different columns numbers comparing to the first line, too
    ; | 2: removing all columns > column numbers in the first line
    ; Return Value ..: Success - 2-dim Array
    ; Failure - 0
    ; @ERROR - 1: error file read
    ; @ERROR - 2: different number of columns / @EXTENDED = CSV-line
    ; - 3: parameter error
    ; Author(s) .....: Thorsten Willert
    ; Date ..........: Mon Dec 07 18:54:35 CET 2009
    ; ==============================================================================
    Func _CSV2Array($hFile, $cSeperator = "auto", $bFilterString = True, $iColumnMode = 0)
    Local $s = FileRead($hFile)
    If @error Then Return SetError(1)

    [/autoit] [autoit][/autoit] [autoit]

    If $cSeperator = Default Then $cSeperator = "auto"
    If Not $cSeperator Then $cSeperator = Opt("GUIDataSeparatorChar")

    [/autoit] [autoit][/autoit] [autoit]

    ; searching the line-seperator and splitting the lines into an array
    Local $aLines
    If StringInStr($s, @CRLF) Then
    $aLines = StringSplit($s, @CRLF, 1)
    ElseIf StringInStr($s, @CR) Then
    $aLines = StringSplit($s, @CR)
    Else
    $aLines = StringSplit($s, @LF)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ; searching the delimiter in the first line
    Local $aTMP
    If $cSeperator = "auto" Then
    Local $iMax = 0
    Local $iC[5] = [0, 0, 0, 0, 0]
    Local $sC[5] = [";", ",", @TAB, "|", " "]

    [/autoit] [autoit][/autoit] [autoit]

    $aTMP = StringRegExp($aLines[1], ";", 3)
    If Not @error Then $iC[0] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], ",", 3)
    If Not @error Then $iC[1] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\t", 3)
    If Not @error Then $iC[2] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "\|", 3)
    If Not @error Then $iC[3] = UBound($aTMP)
    $aTMP = StringRegExp($aLines[1], "[ ]", 3)
    If Not @error Then $iC[4] = UBound($aTMP)

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 0 To UBound($sC) - 1
    If $iC[$i] > $iMax Then
    $iMax = $iC[$i]
    $cSeperator = $sC[$i]
    EndIf
    Next
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ; creating 2-dim array based on the number of data in the first line
    $aTMP = StringSplit($aLines[1], $cSeperator)
    Local $iCol = $aTMP[0]
    Local $aRet[$aLines[0]][$iCol]

    [/autoit] [autoit][/autoit] [autoit]

    ; splitting and filling the lines
    For $i = 1 To $aLines[0]
    $aTMP = StringSplit($aLines[$i], $cSeperator)
    If @error Then ContinueLoop
    If $aTMP[0] > $iCol Then
    Switch $iColumnMode
    Case 0
    Return SetError(2, $i)
    Case 1
    ReDim $aRet[$aLines[0] - 1][$aTMP[0]]
    Case 2
    $aTMP[0] = $iCol
    Case Else
    Return SetError(3)
    EndSwitch
    EndIf
    For $j = 1 To $aTMP[0]
    $aTMP[$j] = StringStripWS($aTMP[$j], 3)
    If $bFilterString Then ; removing leading and trailing " or '
    $aTMP[$j] = StringRegExpReplace($aTMP[$j], '^("|'')(.*?)\1$', '$2')
    EndIf
    $aRet[$i - 1][$j - 1] = $aTMP[$j]
    Next ; /cols
    Next ; /lines

    [/autoit] [autoit][/autoit] [autoit]

    Return $aRet
    EndFunc ;==>_CSV2Array

    [/autoit]


    Funktioniert soweit auch ganz ok - zum Teil immerhin.
    Lese ich eine .csv mit einer Zeilenanzahl von 20 und
    Spaltenanzahl von 20 ein, klappt es einwandfrei.

    Wenn die .csv allerdings über 100 Zeilen und 3 Spalten
    umfasst, gibs da in dem Array Chaos. Der Inhalt der Zeilen
    und Spalten wird zwar auch 100 Zeilen verteilt, allerdings
    auch auf 7 Spalten. Ausserdem werden die ";" mit hinein-
    geschrieben...

    Fehler in der Funktion?

    Gibs da noch andere Möglichkeiten außer die genannte?

    Einmal editiert, zuletzt von Scritch (11. Oktober 2010 um 15:00)

  • Hatte mir ein anderes Beispiel erstellt ebenfalls mit Leerzeichen.
    Da gabs keine Formatierungsschwierigkeiten. Ok, leuchtet
    ein dass das mit den Simikolon dann zu Problemen führt.

    Allerdings sind in dem von mir gezeigen Bsp. keine Simikolons
    enthalten :?:

    Edit: Höchst merkwürdig. Wenn ich die Leerzeichen herausnehme,
    funktionierts. Hmm... ein anderes Dokument funktioniert auch mit
    Leerzeichen...