有個小問題,如下: 現有文本文件1.txt,內容如下: 數值_1出現頻度12647 數值_2出現頻度10000 數值_3出現頻度12608 數值_4出現頻度8712 數值_5出現頻度10658 數值_6出現頻度8472 數值_7出現頻度11232 數值_8出現頻度8648 數值_9出現頻度9264 數值_10出現頻度7192 數值_11出現頻度7192 。。。。 大概有100行 要求把里面每行的數值放到變量中,然后輸出成文本文件 2.txt 舉例: 把第一行的12674,放到變量a1中 把第二行的10000,放到變量a2中 把第三行的12608,放到變量a2中 ….直到最后一行 最后輸出成“2.txt” 文本文件的內容為: a1 = 12647 a2 = 10000 a3 = 12608 a4 = 8712 …. a11 = 7192 希望能能夠找到相關代碼,并且是能在windows下運行的?。≌已秸已秸已?。。 實現代碼如下: VB code: 復制代碼代碼如下:
set fso = createobject("scripting.filesystemobject")
set file=fso.opentextfile("1.txt")
ts = file.readall
file.close
set fil = fso.createtextfile("2.txt")
ts=replace(ts,"數值_","a")
ts=replace(ts,"出現頻度","=")
'''如果有橫線和空行,加上這個,沒有就注釋掉
ts=replace(ts,"-----------------------"+vbnewline+vbnewline,"")
fil.write ts
fil.close
MsgBox "處理完成"上面的代碼是把1.txt直接改成了2.txt,中間變量a1~a100省去了,如果還需要中間變量做其它用途的話,可以讀取2.txt內容并賦值,代碼如下:
VBScript code:
set fso = createobject("scripting.filesystemobject")
set ts = fso.opentextfile("2.txt")
i=0
do while ts.AtEndOfStream=false
str = ts.ReadLine
execute str '執行賦值
i=i+1
execute("value=a" & i)'獲取變量 a1…… 的值
Response.Write("a" & i & "值為:" & value &"<br/>") '輸出
loop還有一種方法,如下面的代碼所示:
VBScript code:
Set fs = CreateObject("Scripting.FileSystemObject")
Set txt1 = fs.OpenTextFile("1.txt", 1)
Set txt2 = fs.CreateTextFile("C:/FSO/ScriptLog.txt")
Do Until txt1.AtEndOfStream
str_a = txt1.ReadLine
str_a = replace(str_a, "度","$")
str_ar = split(str_a, "$")
if isnumeric(str_ar(ubound(str_a))) then
txt2.writeline str_ar(ubound(str_a))
end if
Loop
txt1.close
txt2.close
set txt1 = nothing
set txt2 = nothing
set fs = nothing