Jump to content
Sign in to follow this  
JohnnyDepth

CSV to Array Troubles

Recommended Posts

JohnnyDepth

Hi all,

 

I have been playing around with AutoIt for a couple weeks now, just making random things for fun.  There is one thing I cannot wrap my head around for the life of me..

 

Here is what I am wanting to do:

I have a csv file with this following data/columns:

Hey,Hey1,Hey2,Hey3

Hello,Hello1,Hello2,Hello3

Hi,Hi1,Hi2,Hi3

(let's say this csv file is located C:\Test Folder\Test.csv)

So the csv is three rows(for this example) and has four columns.

 

I cannot figure out how to get this data out and store it into an array that I can use to type each column into 4 different word docs that are open.  I do not need help with moving the mouse to each word doc, but i just don't know how to get the data out, put it into an array.

 

Once I have the data in an array, I know I can figure out how to loop through, but getting to that point is my problem.

 

Any help would be much appreciated.

Share this post


Link to post
Share on other sites
JohnnyDepth

Thanks!  Will that bring in all 4 columns of the csv file?  How do I 'sendkey' each column individually after it has been stored in the array?

Share this post


Link to post
Share on other sites
AutoBert
4 hours ago, JohnnyDepth said:

Thanks!  Will that bring in all 4 columns of the csv file?  

Yes, when your CSV is named test.csv or you change script to your CSV-name. Just test and see: 29_ArrayDisplay.jpg.dc245314e5e458259d2b

4 hours ago, JohnnyDepth said:

How do I 'sendkey' each column individually after it has been stored in the array?

Don't understand, please be more specific about your intention.

Share this post


Link to post
Share on other sites
JohnnyDepth
4 hours ago, AutoBert said:

 

Don't understand, please be more specific about your intention.

Sorry.  I'd like to take the data in an array and type it into certain fields of a website.  So i would want to type column one into a text box field on a website, then column two may be in another text box on a different page of the same website.  Same with column 3 and 4. 

 

I know how to do everything in between such as moving the mouse and waiting for pages to load, but just inserting the individual columns from the csv is what i need help with.

Share this post


Link to post
Share on other sites
AutoBert

I am no native English speaker so please read Language Reference - Variables specialy Arrays, i hope this example sending each elemnt from the array to NotePad helps a little bit:

#include <File.au3>
#include <Array.au3>
Dim $aCSV[1]
_FileReadToArray('test.csv', $aCSV, Default, ',')
_ArrayDisplay($aCSV)
; Run Notepad with the window maximized.
Local $iPID = Run("notepad.exe", "", @SW_SHOWMAXIMIZED)

; Wait 10 seconds for the Notepad window to appear.
$hWnd = WinWait("[CLASS:Notepad]", "", 10)

For $iRows = 1 To $aCSV[0][0] ;starting in row 1 (row 0 contains dimensions info
    For $iCols = 0 To $aCSV[0][1] - 1 ;starting in col 0
        ControlSetText($hWnd, "", "Edit1", 'Row ' & $iRows & '|Col ' & $iCols & ': ' & $aCSV[$iRows][$iCols] & @CRLF)
        Sleep(2000)
    Next ;col
Next ;row

;single element row 2 | col 3
ControlSetText($hWnd, "", "Edit1", @CRLF & 'row 2 | col 3: ' & $aCSV[2][3] & @CRLF)

; Display the text of the edit control.

; Close the Notepad window using the handle returned by WinWait.
WinClose($hWnd)

 

Share this post


Link to post
Share on other sites
JohnnyDepth

Thanks for all your help autobert!  

 

Question, wouldn't the $aCSV[1] need to be $aCSV[3] (assuming it is zero based) since there are four entries per row in the CSV?

Share this post


Link to post
Share on other sites
iamtheky
For $iRows = 1 To $aCSV[0][0]      ;For $iRows = 1 to the Row Count that _FRTA put in row 0 column 0 (making the useful part of the array 1-based) 

For $iCols = 0 To $aCSV[0][1] - 1  ;For $iCols = 0 to the Column Count that _FRTA put in row 0 column 1 minus 1 (because columns still start at 0)

i assume you mean here, because $aCSV is not a 1D array, hopefully those comments help.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
JohnnyDepth
10 hours ago, iamtheky said:
For $iRows = 1 To $aCSV[0][0]      ;For $iRows = 1 to the Row Count that _FRTA put in row 0 column 0 (making the useful part of the array 1-based) 

For $iCols = 0 To $aCSV[0][1] - 1  ;For $iCols = 0 to the Column Count that _FRTA put in row 0 column 1 minus 1 (because columns still start at 0)

i assume you mean here, because $aCSV is not a 1D array, hopefully those comments help.

I apologize for the dumb questions, but what does _FRTA mean in your comments? 

Share this post


Link to post
Share on other sites
iamtheky

FileReadToArray


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
JohnnyDepth

Thank you for all the help!

Share this post


Link to post
Share on other sites
JohnnyDepth

Got it to work perfectly, thanks again for all the help!  For those curious, here is the code:

 

#include <File.au3>
#include <Array.au3>

Global $aCSV[1]
Global $intCount = 0
Global $intRow = 0
Global $intCol = 0
Global $intLineCount = 0

_FileReadToArray("C:\Test Folder\Test CSV.csv", $aCSV, Default, ",")
$intLineCount = _FileCountLines("C:\Test Folder\Test CSV.csv") - 1

While $intCount <= $intLineCount

    $intCount = $intCount + 1
    ;$intRow = $intCount

    MouseClick("primary",63,16,1)
    Send($aCSV[$intRow+$intCount][$intCol])
    Send("{ENTER}")

    Sleep(1000)

    MouseClick("primary",301,16,1)
    Send($aCSV[$intRow+$intCount][$intCol+1])
    Send("{ENTER}")

    Sleep(1000)

    MouseClick("primary",532,16,1)
    Send($aCSV[$intRow+$intCount][$intCol+2])
    Send("{ENTER}")

    Sleep(1000)

WEnd


_ArrayDisplay($aCSV)

 

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  

  • Similar Content

    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
×