我有test.csv文件需要使用autoit读取所有数据
答案 0 :(得分:2)
由于TeamKiller说您的问题很模糊,但这里有一个示例代码,可以让您了解如何阅读CSV文件。
#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
答案 1 :(得分:0)
_ParseCSV()的返回值是二维数组,如
$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
不需要包含
PARAMS:
; _ ParseCSV(“filename”,“,”,“如果发生错误则发出消息”,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