CSV Search AutoIT -
i have csv file contains 4 columns, want search column 2 , change corresponding data in column 4 using autoit:
col 1 col 2 col 3 col 4 1 502 shop 25.00 2 106 house 50.00 3 307 boat 15.00
if columns separated tabs use stringsplit that.
$s1 = '1 502 shop 25.00' $s2 = '2 106 house 50.00' $s3 = '3 307 boat 15.00' $i=1 3 $array = stringsplit(eval('s' & $i), @tab) consolewrite('column 2: "' & stringstripws($array[2], 8) & '"' & @crlf) consolewrite('column 4: "' & stringstripws($array[4], 8) & '"' & @crlf) next
this sample code print:
column 2: "502" column 4: "25.00" column 2: "106" column 4: "50.00" column 2: "307" column 4: "15.00"
edit
this example creates csv file, reads file in , searches every line '106'. if string found , last column has value of '50.00', value replaced '22.00'. result written new csv file.
; write data csv file global $hfile = fileopen('test.csv', 10) if $hfile = -1 exit filewrite($hfile, '1' & @tab & '502 ' & @tab & 'shop' & @tab & '25.00' & @crlf & _ '2' & @tab & '106 ' & @tab & 'house' & @tab & '50.00' & @crlf & _ '3' & @tab & '307' & @tab & 'boat' & @tab & '15.00') fileclose($hfile) ; read csv file , create new 1 if not fileexists('test.csv') exit global $hfilein = fileopen('test.csv') global $hfileout = fileopen('test_new.csv', 10) while 1 global $sline = filereadline($hfilein) if @error = -1 exitloop if stringinstr($sline, '106') $sline = _replaceprices($sline) consolewrite('new price: ' & $sline & @crlf) endif filewriteline($hfileout, $sline) wend fileclose($hfilein) fileclose($hfileout) exit ; search "106" find , corresponding value in ; column 4 (50.00) , change column 4 value "22.00" func _replaceprices($slinefromcsvfile) local $array = stringsplit($slinefromcsvfile, @tab) if stringstripws($array[2], 8) = '106' , _ stringstripws($array[4], 8) = '50.00' return $array[1] & @tab & $array[2] & @tab & _ $array[3] & @tab & '22.00' endif endfunc
if run example result:
Comments
Post a Comment