Jump to content

write multiple columns and rows to csv


gcue
 Share

Recommended Posts

From a multiple column and row array, I am trying to write to a CSV file where each column is seperated by a "," and each row is seperated by a @crlf

here's what I have so far.. and i understand why its putting it all in the same first column.. but I cant figure out how to incorporate the new column at the

uggh brain freeze!!

here's an example array that illustrates what I am trying to do - essentially i want the csv to look like the arraydisplay (columns and rows matching up)

any help is appreciated!

#include <array.au3>

$output_file = @ScriptDir & "\Output.csv"

DeleteFile($output_file)

Local $results_array[2][3]

$results_array[0][0] = "Name"
$results_array[0][1] = "Team"
$results_array[0][2] = "PA"

$results_array[1][0] = "bob"
$results_array[1][1] = "mets"
$results_array[1][2] = "2%"

_ArrayDisplay($results_array)

$rows = UBound($results_array) - 1
$columns = UBound($results_array, 2)

For $x = 0 To $columns - 1
    For $y = 0 To $rows
        FileWrite($output_file, '' & @CRLF & _
                $results_array[$y][$x] & ",")

    Next
Next

ShellExecute($output_file)


Func DeleteFile($file)

    $file_usage = FileOpen($file, 1)

    If $file_usage = -1 Then
        MsgBox(0, @ScriptName, $file & " is in use." & @CRLF & _
                'Please close it before continuing.')
        Exit
    EndIf

    FileClose($file_usage)

    If FileExists($file) Then
        FileDelete($file)
    EndIf

EndFunc   ;==>DeleteFile
Edited by gcue
Link to comment
Share on other sites

This works.

#include <array.au3>

$output_file = @ScriptDir & "\Output.csv"

DeleteFile($output_file)

Local $results_array[2][3]

$results_array[0][0] = "Name"
$results_array[0][1] = "Team"
$results_array[0][2] = "PA"

$results_array[1][0] = "bob"
$results_array[1][1] = "mets"
$results_array[1][2] = "2%"

_ArrayDisplay($results_array)
$rows = UBound($results_array) - 1
$columns = UBound($results_array, 2) -1
#cs
For $x = 0 To $columns - 1
    For $y = 0 To $rows
        FileWrite($output_file, '' & @CRLF & _
                $results_array[$y][$x] & ",")

    Next
Next
#ce
For $i = 0 To $rows
    $sLine = ""
    For $j = 0 To $columns
        $sLine &= $results_array[$i][$j] & ","
    Next
    FileWriteLine($output_file, StringTrimRight($sLine, 1))
Next
ShellExecute("notepad.exe"," " & $output_file)


Func DeleteFile($file)

    $file_usage = FileOpen($file, 1)

    If $file_usage = -1 Then
        MsgBox(0, @ScriptName, $file & " is in use." & @CRLF & _
                'Please close it before continuing.')
        Exit
    EndIf

    FileClose($file_usage)

    If FileExists($file) Then
        FileDelete($file)
    EndIf

EndFunc   ;==>DeleteFile

I have had trouble in the past getting Excel to open files using ShellExecute()

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

not so brilliant. The major change from yours was FileWriteLine() instead of FileWrite() and you needed the -1 for $columns

EDIT:

Actually here it is using FileWrite()

#include <array.au3>

$output_file = @ScriptDir & "\Output.csv"

DeleteFile($output_file)

Local $results_array[2][3]

$results_array[0][0] = "Name"
$results_array[0][1] = "Team"
$results_array[0][2] = "PA"

$results_array[1][0] = "bob"
$results_array[1][1] = "mets"
$results_array[1][2] = "2%"

_ArrayDisplay($results_array)


$rows = UBound($results_array) - 1
$columns = UBound($results_array, 2) -1
#cs
For $x = 0 To $columns - 1
    For $y = 0 To $rows
        FileWrite($output_file, '' & @CRLF & _
                $results_array[$y][$x] & ",")

    Next
Next
#ce
Local $sOut = ""
For $i = 0 To $rows
    $sLine = ""
    For $j = 0 To $columns
        $sLine &= $results_array[$i][$j] & ","
    Next
    $sOut &= StringTrimRight($sLine, 1)
Next
FileWrite($output_file, StringStripWS($sOut, 2))
ShellExecute("notepad.exe"," " & $output_file)


Func DeleteFile($file)

    $file_usage = FileOpen($file, 1)

    If $file_usage = -1 Then
        MsgBox(0, @ScriptName, $file & " is in use." & @CRLF & _
                'Please close it before continuing.')
        Exit
    EndIf

    FileClose($file_usage)

    If FileExists($file) Then
        FileDelete($file)
    EndIf

EndFunc   ;==>DeleteFile
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...