使用AutoIt在循环中解析CSV文件的所有行

时间:2012-12-17 05:14:32

标签: autoit

我有以下代码来读取包含两行数据的.csv文件。我不知道它有什么问题。如何改进它以读取包含两行数据的.csv文件?

#include <GUIConstants.au3>
#include <string.au3>

$file = FileOpen("test.csv", 0)

If $file = -1 Then
    MsgBox(0, "error", "File doesn't exist or can't be read")
    Exit
EndIf

$string = (FileReadLine($file, 1))
$input = StringSplit($string, ",", 1)
$input = StringSplit($string, ",", 1)
Local $value1 = $input[1]
ConsoleWrite("var=" & $value1)

3 个答案:

答案 0 :(得分:2)

_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:

  1. 文件名
  2. 定界符
  3. 如果无法打开文件则显示消息
  4. 逻辑true / false跳过文件的第一行
  5. ; _ 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
    

答案 1 :(得分:0)

这是一个程序,它将读取包含2行的CSV文件并输出两个数据字段。 如果有更多行,则将输入数组更改为更大。

#include <GUIConstants.au3>
#include <string.au3>

$file = FileOpen("test.csv", 0)

If $file = -1 Then
   MsgBox(0, "error", "File doesn't exist or can't be read")
   Exit
EndIf

   ; Read in lines of text until the EOF is reached
While 1
    Local $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
   $input = StringSplit($line, ",")

   ;This is reading fields 1 and 2. 
   ;The array index [0] 
   ;it will tell you how big the array is.
   Local $value1 = $input[1]
   Local $value2 = $input[2]
   ConsoleWrite("var=" & $value1)
   ConsoleWrite("var=" & $value2)

WEnd

答案 2 :(得分:0)

至于解析CSV文件,最好使用库(用户定义的函数),尤其是在您例如包含带有引号的字符串(“单元格” /字符串内的逗号)或换行符的复杂CSV,很难处理。

我能推荐的最好的是CSVSplit。 基本上,您有一个函数_CSVSplit,该函数接收整个CSV文件(内容,即字符串!),并返回一个二维数组:

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

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

然后,您可以使用此数组执行所有所需的操作。显然, CSVSplit 还提供了“反向”功能,可以将数组再次变成CSV字符串_ArrayToCSV

相关问题