Jump to content
Sign in to follow this  
tbaror

CSV FILE to 2D Dim?

Recommended Posts

tbaror

Hello,

i am trying to read CSV delimited with "," file and insert it into 2D i have tried to play with LibCsv2.au3 but keeps getting error.

i wonder if there is any other function to deal with csv files.

thanks

Share this post


Link to post
Share on other sites
DaleHohm

Hello,

i am trying to read CSV delimited with "," file and insert it into 2D i have tried to play with LibCsv2.au3 but keeps getting error.

i wonder if there is any other function to deal with csv files.

thanks

Ivan created one as well. Search the forum.

I've used LibCsv2.au3 without any trouble, so there may be trouble with your source file or with how you are trying to use it.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites
Malkey

Hello,

i am trying to read CSV delimited with "," file and insert it into 2D i have tried to play with LibCsv2.au3 but keeps getting error.

i wonder if there is any other function to deal with csv files.

thanks

I am not sure if this will help, but from here

http://www.autoitscript.com/forum/index.ph...st&p=474934

I extracted this

; Save matrix to disk. csv file extension
Func savemat($mainfrm, $m)
    GUISetState (@SW_ENABLE,$mainfrm )
    $Pathfile = FileSaveDialog( "Choose a name.",@WorkingDir,"Comma-Separated Variables files (*.csv)|All (*.*)",8) 
    If @error Then
        ;MsgBox(4096,"","No File(s) chosen") 
        $p = 1
        return $p
        ;MsgBox(0,"return -1", $p)
    EndIf
    dim $r = UBound($m), $c = UBound($m, 2)
    
    if StringRight($Pathfile,4) <> ".csv" then $Pathfile = $Pathfile & ".csv"
        $filename = StringTrimLeft($Pathfile,StringLen(@WorkingDir)+1)  
        $hfile = FileOpen($Pathfile, 2)
        $Line = ""
        $Counter = 0
        for $x = 0 to $r -1
            for $y = 0 to $c -1
                if $mainfrm = "Matrix 1" Then 
                    $COMMALESS = StringReplace (GUICtrlRead($run1[$Counter]),",","{COMMA}")     
                    $Line = $Line & $COMMALESS & ","
                EndIf
                if $mainfrm = "Matrix 2" Then 
                    $COMMALESS = StringReplace (GUICtrlRead($run2[$Counter]),",","{COMMA}")     
                    $Line = $Line & $COMMALESS & ","
                EndIf   
                if $mainfrm = "Matrix 3" Then 
                    $COMMALESS = StringReplace (GUICtrlRead($run3[$Counter]),",","{COMMA}")     
                    $Line = $Line & $COMMALESS & ","
                EndIf                       
                $Counter += 1
            Next
            FileWriteLine($hfile, StringTrimRight($Line,1) & @CRLF)
            $Line = ""
        next                
        FileClose($hfile)
        $p = 0
        ;MsgBox(0,"End", $p)
        Return $p       
EndFunc
    
; Open file stored on disk.  csv file extension
;  $Pathfile - path and name of file to be opened
; If $Pathfile not supplied, FileOpenDialog will allow the choosing a file to be open.
Func fileop($Pathfile = "") 
    If $Pathfile = "" Then      
        $Pathfile = FileOpenDialog( "Choose a name.",@WorkingDir,"Comma-Separated Variables files (*.csv)|All (*.*)",8)
    EndIf       
    If @error Then
        ;MsgBox(4096,"","No File(s) chosen") 
        $m = 1
        return $m
    EndIf
    $filename = StringTrimLeft($Pathfile,StringLen(@WorkingDir)+1)
    $hfile = FileOpen($Pathfile, 0)
    $colcount = 1
    $line = FileReadLine($hfile)
    for $ch = 1 to StringLen($line)
        if StringMid($line, $ch,1) = "," then $colcount += 1    
            ;MsgBox(0,"","rowcount "& $rowcount & "      colcount " & $colcount)            
    next
    $rowcount = 1
    While 1 
        $line = FileReadLine($hfile)
        If @error = -1 Then ExitLoop    
        $rowcount += 1
    Wend
    ;MsgBox(0,"","rowcount "& $rowcount & "      colcount " & $colcount)
    FileClose($hfile)
    $m = MatCreate($rowcount, $colcount)
    ;MsgBox(0,"","$rowcount=" & $rowcount & "  $colcount="  & $colcount)
    $hfile = FileOpen($Pathfile, 0)
    for $x = 0 to $rowcount - 1
        $line = StringSplit (FileReadLine($hfile),",")
        for $y = 0 to $colcount - 1 
            $m[$x][$y] = StringReplace ($line[$y+1],"{COMMA}",",")                  
            ;MsgBox(0,"","$x=" & $x & "  $y="  & $y &  "  $m[$x][$y]=" & $m[$x][$y] )                       
        Next        
    next    
    FileClose($hfile)
     return $m
 EndFunc

Because I wanted commas in the data, when saving to file, data commas became "{COMMA}"

When reading the saved file, the data went through StringReplace () replacing "{COMMA}" with ",".

I am certain there are other CSV related functions on the forum. I hope this one helps.

Edit: I just found the LibCsv2.au3 file at

http://www.autoitscript.com/forum/index.ph...st&p=383317

It looks very good.

Edited by Malkey

Share this post


Link to post
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
Sign in to follow this  

×