搜索并比较.CSV文件的值

搜索并比较.CSV文件的值,csv,autoit,Csv,Autoit,我的目标是搜索CSV的第一列两次,然后执行一个操作(取决于同一记录的第三列中的值)。我从VBScript开始使用InStr(): 它可以将siteDist的值与第二次搜索的相同数据进行比较。但是,我更喜欢使用AutoIt。有没有办法使用AutoIt(或实现我的计划的命令)来实现这一点 我用于测试的简单CSV文件: Site,District,Region 1,1,1 2,1,1 3,1,2 4,2,2 5,2,1 在站点中搜索两个单独的条目,然后确认地区匹配,在站点1运行此脚本时,对于站点1、

我的目标是搜索CSV的第一列两次,然后执行一个操作(取决于同一记录的第三列中的值)。我从VBScript开始使用
InStr()

它可以将
siteDist
的值与第二次搜索的相同数据进行比较。但是,我更喜欢使用AutoIt。有没有办法使用AutoIt(或实现我的计划的命令)来实现这一点

我用于测试的简单CSV文件:

Site,District,Region
1,1,1
2,1,1
3,1,2
4,2,2
5,2,1
站点
中搜索两个单独的条目,然后确认
地区
匹配,在站点1运行此脚本时,对于
站点
1、2或3,应将其评估为true,对于
站点
4和5,应将其评估为false。

使用以下方法:

; #FUNCTION# ====================================================================================================================
; Name...........: _ParseCSV
; Description ...: Reads a CSV-file
; Syntax.........: _ParseCSV($sFile, $sDelimiters=',', $sQuote='"', $iFormat=0)
; Parameters ....: $sFile       - File to read or string to parse
;                  $sDelimiters - [optional] Fieldseparators of CSV, mulitple are allowed (default: ,;)
;                  $sQuote      - [optional] Character to quote strings (default: ")
;                  $iFormat     - [optional] Encoding of the file (default: 0):
;                  |-1     - No file, plain data given
;                  |0 or 1 - automatic (ASCII)
;                  |2      - Unicode UTF16 Little Endian reading
;                  |3      - Unicode UTF16 Big Endian reading
;                  |4 or 5 - Unicode UTF8 reading
; Return values .: Success - 2D-Array with CSV data (0-based)
;                  Failure - 0, sets @error to:
;                  |1 - could not open file
;                  |2 - error on parsing data
;                  |3 - wrong format chosen
; Author ........: ProgAndy
; Modified.......:
; Remarks .......:
; Related .......: _WriteCSV
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _ParseCSV($sFile, $sDelimiters=',;', $sQuote='"', $iFormat=0)
    Local Static $aEncoding[6] = [0, 0, 32, 64, 128, 256]
    If $iFormat < -1 Or $iFormat > 6 Then
        Return SetError(3,0,0)
    ElseIf $iFormat > -1 Then
        Local $hFile = FileOpen($sFile, $aEncoding[$iFormat]), $sLine, $aTemp, $aCSV[1], $iReserved, $iCount
        If @error Then Return SetError(1,@error,0)
        $sFile = FileRead($hFile)
        FileClose($hFile)
    EndIf
    If $sDelimiters = "" Or IsKeyword($sDelimiters) Then $sDelimiters = ',;'
    If $sQuote = "" Or IsKeyword($sQuote) Then $sQuote = '"'
    $sQuote = StringLeft($sQuote, 1)
    Local $srDelimiters = StringRegExpReplace($sDelimiters, '[\\\^\-\[\]]', '\\\0')
    Local $srQuote = StringRegExpReplace($sQuote, '[\\\^\-\[\]]', '\\\0')
    Local $sPattern = StringReplace(StringReplace('(?m)(?:^|[,])\h*(["](?:[^"]|["]{2})*["]|[^,\r\n]*)(\v+)?',',', $srDelimiters, 0, 1),'"', $srQuote, 0, 1)
    Local $aREgex = StringRegExp($sFile, $sPattern, 3)
    If @error Then Return SetError(2,@error,0)
    $sFile = '' ; save memory
    Local $iBound = UBound($aREgex), $iIndex=0, $iSubBound = 1, $iSub = 0
    Local $aResult[$iBound][$iSubBound]
    For $i = 0 To $iBound-1
        Select
            Case StringLen($aREgex[$i])<3 And StringInStr(@CRLF, $aREgex[$i])
                $iIndex += 1
                $iSub = 0
                ContinueLoop
            Case StringLeft(StringStripWS($aREgex[$i], 1),1)=$sQuote
                $aREgex[$i] = StringStripWS($aREgex[$i], 3)
                $aResult[$iIndex][$iSub] = StringReplace(StringMid($aREgex[$i], 2, StringLen($aREgex[$i])-2), $sQuote&$sQuote, $sQuote, 0, 1)
            Case Else
                $aResult[$iIndex][$iSub] = $aREgex[$i]
        EndSelect
        $aREgex[$i]=0 ; save memory
        $iSub += 1
        If $iSub = $iSubBound Then
            $iSubBound += 1
            ReDim $aResult[$iBound][$iSubBound]
        EndIf
    Next
    If $iIndex = 0 Then $iIndex=1
    ReDim $aResult[$iIndex][$iSubBound]
    Return $aResult
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _WriteCSV
; Description ...: Writes a CSV-file
; Syntax.........: _WriteCSV($sFile, Const ByRef $aData, $sDelimiter, $sQuote, $iFormat=0)
; Parameters ....: $sFile      - Destination file
;                  $aData      - [Const ByRef] 0-based 2D-Array with data
;                  $sDelimiter - [optional] Fieldseparator (default: ,)
;                  $sQuote     - [optional] Quote character (default: ")
;                  $iFormat    - [optional] character encoding of file (default: 0)
;                  |0 or 1 - ASCII writing
;                  |2      - Unicode UTF16 Little Endian writing (with BOM)
;                  |3      - Unicode UTF16 Big Endian writing (with BOM)
;                  |4      - Unicode UTF8 writing (with BOM)
;                  |5      - Unicode UTF8 writing (without BOM)
; Return values .: Success - True
;                  Failure - 0, sets @error to:
;                  |1 - No valid 2D-Array
;                  |2 - Could not open file
; Author ........: ProgAndy
; Modified.......:
; Remarks .......:
; Related .......: _ParseCSV
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _WriteCSV($sFile, Const ByRef $aData, $sDelimiter=',', $sQuote='"', $iFormat=0)
    Local Static $aEncoding[6] = [2, 2, 34, 66, 130, 258]
    If $sDelimiter = "" Or IsKeyword($sDelimiter) Then $sDelimiter = ','
    If $sQuote = "" Or IsKeyword($sQuote) Then $sQuote = '"'
    Local $iBound = UBound($aData, 1), $iSubBound = UBound($aData, 2)
    If Not $iSubBound Then Return SetError(2,0,0)
    Local $hFile = FileOpen($sFile, $aEncoding[$iFormat])
    If @error Then Return SetError(2,@error,0)
    For $i = 0 To $iBound-1
        For $j = 0 To $iSubBound-1
            FileWrite($hFile, $sQuote & StringReplace($aData[$i][$j], $sQuote, $sQuote&$sQuote, 0, 1) & $sQuote)
            If $j < $iSubBound-1 Then FileWrite($hFile, $sDelimiter)
        Next
        FileWrite($hFile, @CRLF)
    Next
    FileClose($hFile)
    Return True
EndFunc

; === EXAMPLE ===================================================
;~  #include<Array.au3>
;~  $aResult = _ParseCSV(@ScriptDir & '\test.csv', "\", '$', 4)
;~  _ArrayDisplay($aResult)
;~  _WriteCSV(@ScriptDir & '\written.csv', $aResult, ',', '"', 5)
; ===============================================================
#功能#====================================================================================================================
; 姓名………..:\u
; 描述…:读取CSV文件
; 语法………:_ParseCSV($sFile,$sDelimiters=',',$sQuote=',$iFormat=0)
;参数….:$sFile-要读取的文件或要分析的字符串
;$sDelimiters-[可选]允许使用多个CSV字段分隔符(默认值:,;)
;$sQuote-[可选]字符以引用字符串(默认值:)
;                  $iFormat-[可选]文件编码(默认值:0):
;                  |-1-无文件,给出了普通数据
;                  |0或1-自动(ASCII)
;                  |2-Unicode UTF16小端读取
;                  |3-Unicode UTF16大端读取
;                  |4或5-Unicode UTF8读取
; 返回值:Success-包含CSV数据的二维数组(基于0)
;                  失败-0,将@error设置为:
;                  |1-无法打开文件
;                  |2-解析数据时出错
;                  |3-选择了错误的格式
; 作者:ProgAndy
; 被改进的。。。。。。。:
; 评论。。。。。。。:
; 相关……:\u WriteCSV
; 链接………:
; 例子。。。。。。。:
; ===============================================================================================================================
Func _ParseCSV($sFile,$sDelimiters=',;',$sQuote='',$iFormat=0)
本地静态$aEncoding[6]=[0,0,32,64,128,256]
如果$iFormat<-1或$iFormat>6,则
返回设置错误(3,0,0)
如果$iFormat>-1则
本地$hFile=FileOpen($sFile,$aencode[$iFormat]),$sLine,$aTemp,$aCSV[1],$iReserved,$iCount
如果@error,则返回SetError(1,@error,0)
$sFile=FileRead($hFile)
文件关闭($hFile)
恩迪夫
如果$sDelimiters=”“或IsKeyword($sDelimiters),则$sDelimiters=',;'
如果$sQuote=”“或IsKeyword($sQuote),则$sQuote='“'
$sQuote=StringLeft($sQuote,1)
本地$srDelimiters=StringRegExpReplace($sDelimiters,[\\\^\-\[\]],“\\\0”)
本地$srQuote=StringRegExpReplace($sQuote,[\\\^\-\[\]],“\\\0”)
本地$sPattern=StringReplace(StringReplace('(?m)(?:^ |[,])\h*([“](?:[^”]|[“]{2})*[“]|[^、\r\n]*)(\v+),',',$srDelimiters,0,1),“,$srQuote,0,1)
本地$aREgex=StringRegExp($sFile,$sPattern,3)
如果@error,则返回SetError(2,@error,0)
$sFile='';保存内存
本地$iBound=UBound($aREgex),$iIndex=0,$iSubBound=1,$iSub=0
本地$aResult[$iBound][$iSubBound]
对于$i=0到$iBound-1
挑选
案例字符串($aREgex[$i])
…是否有其他方法来实现我在AutoIT中的最终目标,或者是否有一个直接等效的命令集来实现我的原始计划

示例使用(用所需的任何操作替换
ConsoleWrite()
):


您还应该包含.csv文件,以便我们进行测试。否则我们将在黑暗中拍摄。在AutoITI中,我可以使用_FileReadToArray将数据获取到数组中,但从那里所有的_数组查询都返回位置信息而不是值。如果我在上面的示例中搜索site 1,它会返回并告诉我在第2行第1列中找到了它。我无法让它从该行返回任何其他数据值。(在下一篇评论中继续)我正在尝试使用它,然后根据发现的行进行第二次搜索,但我再次遇到了同样的问题-它只会返回第2行第2列-而不是数据(1)包含在其中。这就是我被卡住的地方。我无法在AutoIT中找到一个查询,它将执行VBS所做的操作,并从找到的行中向我提供数据,而不仅仅是索引信息。问题出在哪里?如果你有索引,那么获取值就没有问题。只需使用:$arrayName[row][cloumn]然后用consolewrite或MsgBox打印出来,或者在比较语句中使用它。。。
; #FUNCTION# ====================================================================================================================
; Name...........: _ParseCSV
; Description ...: Reads a CSV-file
; Syntax.........: _ParseCSV($sFile, $sDelimiters=',', $sQuote='"', $iFormat=0)
; Parameters ....: $sFile       - File to read or string to parse
;                  $sDelimiters - [optional] Fieldseparators of CSV, mulitple are allowed (default: ,;)
;                  $sQuote      - [optional] Character to quote strings (default: ")
;                  $iFormat     - [optional] Encoding of the file (default: 0):
;                  |-1     - No file, plain data given
;                  |0 or 1 - automatic (ASCII)
;                  |2      - Unicode UTF16 Little Endian reading
;                  |3      - Unicode UTF16 Big Endian reading
;                  |4 or 5 - Unicode UTF8 reading
; Return values .: Success - 2D-Array with CSV data (0-based)
;                  Failure - 0, sets @error to:
;                  |1 - could not open file
;                  |2 - error on parsing data
;                  |3 - wrong format chosen
; Author ........: ProgAndy
; Modified.......:
; Remarks .......:
; Related .......: _WriteCSV
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _ParseCSV($sFile, $sDelimiters=',;', $sQuote='"', $iFormat=0)
    Local Static $aEncoding[6] = [0, 0, 32, 64, 128, 256]
    If $iFormat < -1 Or $iFormat > 6 Then
        Return SetError(3,0,0)
    ElseIf $iFormat > -1 Then
        Local $hFile = FileOpen($sFile, $aEncoding[$iFormat]), $sLine, $aTemp, $aCSV[1], $iReserved, $iCount
        If @error Then Return SetError(1,@error,0)
        $sFile = FileRead($hFile)
        FileClose($hFile)
    EndIf
    If $sDelimiters = "" Or IsKeyword($sDelimiters) Then $sDelimiters = ',;'
    If $sQuote = "" Or IsKeyword($sQuote) Then $sQuote = '"'
    $sQuote = StringLeft($sQuote, 1)
    Local $srDelimiters = StringRegExpReplace($sDelimiters, '[\\\^\-\[\]]', '\\\0')
    Local $srQuote = StringRegExpReplace($sQuote, '[\\\^\-\[\]]', '\\\0')
    Local $sPattern = StringReplace(StringReplace('(?m)(?:^|[,])\h*(["](?:[^"]|["]{2})*["]|[^,\r\n]*)(\v+)?',',', $srDelimiters, 0, 1),'"', $srQuote, 0, 1)
    Local $aREgex = StringRegExp($sFile, $sPattern, 3)
    If @error Then Return SetError(2,@error,0)
    $sFile = '' ; save memory
    Local $iBound = UBound($aREgex), $iIndex=0, $iSubBound = 1, $iSub = 0
    Local $aResult[$iBound][$iSubBound]
    For $i = 0 To $iBound-1
        Select
            Case StringLen($aREgex[$i])<3 And StringInStr(@CRLF, $aREgex[$i])
                $iIndex += 1
                $iSub = 0
                ContinueLoop
            Case StringLeft(StringStripWS($aREgex[$i], 1),1)=$sQuote
                $aREgex[$i] = StringStripWS($aREgex[$i], 3)
                $aResult[$iIndex][$iSub] = StringReplace(StringMid($aREgex[$i], 2, StringLen($aREgex[$i])-2), $sQuote&$sQuote, $sQuote, 0, 1)
            Case Else
                $aResult[$iIndex][$iSub] = $aREgex[$i]
        EndSelect
        $aREgex[$i]=0 ; save memory
        $iSub += 1
        If $iSub = $iSubBound Then
            $iSubBound += 1
            ReDim $aResult[$iBound][$iSubBound]
        EndIf
    Next
    If $iIndex = 0 Then $iIndex=1
    ReDim $aResult[$iIndex][$iSubBound]
    Return $aResult
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _WriteCSV
; Description ...: Writes a CSV-file
; Syntax.........: _WriteCSV($sFile, Const ByRef $aData, $sDelimiter, $sQuote, $iFormat=0)
; Parameters ....: $sFile      - Destination file
;                  $aData      - [Const ByRef] 0-based 2D-Array with data
;                  $sDelimiter - [optional] Fieldseparator (default: ,)
;                  $sQuote     - [optional] Quote character (default: ")
;                  $iFormat    - [optional] character encoding of file (default: 0)
;                  |0 or 1 - ASCII writing
;                  |2      - Unicode UTF16 Little Endian writing (with BOM)
;                  |3      - Unicode UTF16 Big Endian writing (with BOM)
;                  |4      - Unicode UTF8 writing (with BOM)
;                  |5      - Unicode UTF8 writing (without BOM)
; Return values .: Success - True
;                  Failure - 0, sets @error to:
;                  |1 - No valid 2D-Array
;                  |2 - Could not open file
; Author ........: ProgAndy
; Modified.......:
; Remarks .......:
; Related .......: _ParseCSV
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _WriteCSV($sFile, Const ByRef $aData, $sDelimiter=',', $sQuote='"', $iFormat=0)
    Local Static $aEncoding[6] = [2, 2, 34, 66, 130, 258]
    If $sDelimiter = "" Or IsKeyword($sDelimiter) Then $sDelimiter = ','
    If $sQuote = "" Or IsKeyword($sQuote) Then $sQuote = '"'
    Local $iBound = UBound($aData, 1), $iSubBound = UBound($aData, 2)
    If Not $iSubBound Then Return SetError(2,0,0)
    Local $hFile = FileOpen($sFile, $aEncoding[$iFormat])
    If @error Then Return SetError(2,@error,0)
    For $i = 0 To $iBound-1
        For $j = 0 To $iSubBound-1
            FileWrite($hFile, $sQuote & StringReplace($aData[$i][$j], $sQuote, $sQuote&$sQuote, 0, 1) & $sQuote)
            If $j < $iSubBound-1 Then FileWrite($hFile, $sDelimiter)
        Next
        FileWrite($hFile, @CRLF)
    Next
    FileClose($hFile)
    Return True
EndFunc

; === EXAMPLE ===================================================
;~  #include<Array.au3>
;~  $aResult = _ParseCSV(@ScriptDir & '\test.csv', "\", '$', 4)
;~  _ArrayDisplay($aResult)
;~  _WriteCSV(@ScriptDir & '\written.csv', $aResult, ',', '"', 5)
; ===============================================================
#region    ;************ Includes ************
#include <Array.au3>
#endregion    ;************ Includes ************
; _csvTo2DArray

Local $re = _csvTo2DArray("c:\Repository.csv", ';')
_ArrayDisplay($re)

Func _csvTo2DArray($file, $delim = ',')
    Local $content = FileRead($file)
    Local $rows_A = StringSplit(StringStripCR($content), @LF, 2)
    StringReplace($rows_A[0], $delim, $delim)
    Local $countColumns = @extended
    Local $columns_A = 0

    Local $2D_A[UBound($rows_A)][$countColumns + 1]

    For $z = 0 To UBound($rows_A) - 1
        $columns_A = StringSplit($rows_A[$z], $delim, 2)
        For $y = 0 To UBound($columns_A) - 1
            $2D_A[$z][$y] = $columns_A[$y]
        Next
    Next
    Return $2D_A
EndFunc   ;==>_csvTo2DArray
#include <FileConstants.au3>
#include <File.au3>
#include <Array.au3>

Global Enum  $CSV_COL_SITE, _
             $CSV_COL_DISTRICT, _
             $CSV_COL_REGION

Global Const $g_sFileCSV   = 'C:\bin\roster.csv'
Global Const $g_sFileDelim = ','
Global Const $g_iColSearch = $CSV_COL_DISTRICT
Global Const $g_sValSearch = '1'
Global Const $g_sMessage   = 'Matched row #%i : %s\n'

Global       $g_iRow       = 0
Global       $g_aCSV

_FileReadToArray($g_sFileCSV, $g_aCSV, $FRTA_NOCOUNT, $g_sFileDelim)

While True

    $g_iRow = _ArraySearch($g_aCSV, $g_sValSearch, ($g_iRow ? $g_iRow + 1 : $g_iRow), 0, 0, 0, 1, $g_iColSearch, False)
    If @error Then ExitLoop

    ConsoleWrite(StringFormat($g_sMessage, $g_iRow, _ArrayToString($g_aCSV, $g_sFileDelim, $g_iRow, $g_iRow, '')))

WEnd
Matched row #2 : 1,1,1
Matched row #3 : 2,1,1
Matched row #4 : 3,1,2