-1

I have test.csv file need to read all the data using autoit

user1909028
  • 19
  • 1
  • 2
  • 4
  • Go and try [FileReadLine](http://www.autoitscript.com/autoit3/docs/functions/FileReadLine.htm) and try to use [StringSplit](http://www.autoitscript.com/autoit3/docs/functions/StringSplit.htm) afterwards. – Samoth Dec 18 '12 at 08:54
  • 2
    possible duplicate of [Parse all the rows of a CSV file in a loop using AutoIt](http://stackoverflow.com/questions/13908625/parse-all-the-rows-of-a-csv-file-in-a-loop-using-autoit) –  Dec 15 '13 at 14:18

3 Answers3

2

As TeamKiller said your question is quite vague but here is a sample code that should give you an idea of how to read a CSV file.

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

Opt("MustDeclareVars", 1)

Global Const $CSVFILE = "C:\Temp\test.csv"
Global Const $DELIM = ";" ;the delimiter in the CSV file
Global $i, $arrContent, $arrLine, $res = 0

$res = _FileReadToArray($CSVFILE, $arrContent)
If $res = 1 Then
    For $i = 1 To $arrContent[0]
        $arrLine = StringSplit($arrContent[$i], $DELIM)
        If IsArray($arrLine) And $arrLine[0]<>0 Then
            _ArrayDisplay($arrLine)
            ; do something with the elements of the line
        Else
            MsgBox(48, "", "Error splitting line!")        
        EndIf
    Next 
Else
    MsgBox(48, "", "Error opening file!")
EndIf
Andreas
  • 5,393
  • 9
  • 44
  • 53
0

return value of _ParseCSV() is 2D array like

$array[1][1] first line first data
$array[1][2] first line second data
$array[3][5] 3rd line 5th data

$array[0][0] gives number of lines
$array[0][1] gives number of data in each line

no need for includes

params:

  1. filename
  2. delimeter
  3. message display if cannot open file
  4. logical true/false to skip the first line of the file

;_ParseCSV("filename",",","message if error happens",true)

Func _ParseCSV($f,$Dchar,$error,$skip)

  Local $array[500][500]
  Local $line = ""

  $i = 0
  $file = FileOpen($f,0)
  If $file = -1 Then
    MsgBox(0, "Error", $error)
    Return False
   EndIf

  ;skip 1st line
  If $skip Then $line = FileReadLine($file)

  While 1
       $i = $i + 1
       Local $line = FileReadLine($file)
       If @error = -1 Then ExitLoop
       $row_array = StringSplit($line,$Dchar)
        If $i == 1 Then $row_size = UBound($row_array) 
        If $row_size <> UBound($row_array) Then  MsgBox(0, "Error", "Row: " & $i & " has different size ")
        $row_size = UBound($row_array)
        $array = _arrayAdd_2d($array,$i,$row_array,$row_size)

   WEnd
  FileClose($file)
  $array[0][0] = $i-1 ;stores number of lines
   $array[0][1] = $row_size -1  ; stores number of data in a row (data corresponding to index 0 is the number of data in a row actually that's way the -1)
   Return $array

EndFunc
Func _arrayAdd_2d($array,$inwhich,$row_array,$row_size)
    For $i=1 To $row_size -1 Step 1
        $array[$inwhich][$i] = $row_array[$i]
  Next
  Return $array
   EndFunc
Chris
  • 195
  • 1
  • 13
-1

As for parsing a CSV file, you are likely better off using a library (called user-defined functions in AutoIt), especially if you e.g. have complex CSVs with quoted strings (commas inside of the "cell"/string) or line breaks, which are hard to handle.

The best I can recommend is CSVSplit. Basically you have a function _CSVSplit that takes a whole CSV file (content, i.e. string!) and returns you a two-dimensional array:

Local $sCSV = FileRead($sFilePath)
If @error Then ; ....

$aSplitArray = _CSVSplit($sCSV, ",")

You can then do everything you want with this array. Obviously, CSVSplit also provides the "reverse" function for turning an array into a CSV string again, _ArrayToCSV.


Originally posted as an answer here, which I consider a duplicate of this question.

rugk
  • 4,755
  • 2
  • 28
  • 55