stringreplace後不能寫入文件?
本帖最后由 wongfung 于 2009-4-30 13:50 编辑我想將這個文件
-----------------
id="1"
aaa
bbb
id="12"
aaa
bbb
id="123"
aaa
bbb
-----------------
變成
-----------------
001Line1
001Line2
012Line1
012Line2
123Line1
123Line2
-----------------
於是寫了以下代碼:
#Include <File.au3>
#include <Array.au3>
$file = FileOpenDialog("Open", @WorkingDir, "All Files(*.*)" , 1 )
FileCopy($file, $file & "_converted", 1)
Local $aLine, $id
_FileReadToArray($file & "_converted", $aLine)
$i = 1
While $i < UBound($aLine)
If Stringleft($aLine[$i], 4) = 'id="' Then
$id = StringTrimRight ( $aLine[$i], 1 )
$id = StringMid($id, 5)
$id = StringRight("000" & $id, 3)
$line1 = StringReplace($aLine[$i+1], "aaa", $id & "Line1" )
$line2 = StringReplace($aLine[$i+2], "bbb", $id & "Line2" )
_ArrayDelete($aLine, $i)
Else
$i += 2
EndIf
WEnd
_FileWriteFromArray($file & "_converted", $aLine, 1)
MsgBox( 0, "", "OK")
結果只有id那行給刪掉, 但aaa跟bbb沒有給replace...
請問如何解決???
代碼應如何改寫??? 。。。暴汗,还是这个问题,三天了都没解决?
文件读入到数组后,修改就是改$aLine[$i]的值。仔细想想吧。
#Include <File.au3>
#include <Array.au3>
$file = FileOpenDialog("Open", @WorkingDir, "All Files(*.*)" , 1 )
FileCopy($file, $file & "_converted", 1)
Local $aLine, $id, $iLine
_FileReadToArray($file & "_converted", $aLine)
For $i = 1 To $aLine
If StringLeft($aLine[$i], 4) = 'id="' Then
$id = StringTrimRight($aLine[$i], 1)
$id = StringMid($id, 5)
$id = StringRight("000" & $id, 3)
$iLine = 1
$aLine[$i] = ""
ElseIf $id <> "" Then
$aLine[$i] = $id & "Line" & $iLine
$iLine += 1
EndIf
Next
_FileWriteFromArray($file & "_converted", $aLine, 1)
MsgBox( 0, "", "OK") 。。。暴汗,还是这个问题,三天了都没解决?
文件读入到数组后,修改就是改$aLine[$i]的值。仔细想想吧。
#Include <File.au3>
#include <Array.au3>
$file = FileOpenDialog("Open", @WorkingDir, "All Files(*.*)" , 1 )
FileCopy($file, $file & "_converted", 1)
Local $aLine, $id, $iLine
_FileReadToArray($file & "_converted", $aLine)
For $i = 1 To $aLine
If StringLeft($aLine[$i], 4) = 'id="' Then
$id = StringTrimRight($aLine[$i], 1)
$id = StringMid($id, 5)
$id = StringRight("000" & $id, 3)
$iLine = 1
$aLine[$i] = ""
ElseIf $id <> "" Then
$aLine[$i] = $id & "Line" & $iLine
$iLine += 1
EndIf
Next
_FileWriteFromArray($file & "_converted", $aLine, 1)
MsgBox( 0, "", "OK") 本帖最后由 wongfung 于 2009-4-30 17:50 编辑
。。。暴汗,还是这个问题,三天了都没解决?
文件读入到数组后,修改就是改$aLine[$i]的值。仔细想想吧。
#Include
#include
$file = FileOpenDialog("Open", @WorkingDir, "All Files(*.*)" , 1 )
File ...
sensel 发表于 2009-4-29 02:51 http://www.autoitx.com/images/common/back.gif
原來要定義回aline[$i]是甚麼的啊
明白了...
不過下面的條件不太明白???
$aLine[$i] = ""
ElseIf $id <> "" Then
页:
[1]