如何编辑CSV文件中的记录?
例如,我有一个名为“test.csv”的.csv文件,里面是:
"123","Active" "456","Not-Active" "999000123","Active"
如何修改"456"
并将其从Not-Active
更改为Active
我能想到的唯一方法是:
"456","
。"456","
的行位置。怎么做?但有没有更简单的方法呢?
如果不是,我该如何做第4,5和6步?
也许将其转换为数组或其他东西?但我不知道如何在Classic ASP中做到这一点。
答案 0 :(得分:1)
script @abr mentioned的简化版本:
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim tsIn : Set tsIn = goFS.OpenTextFile("..\data\46734115.csv")
Dim tsOut : Set tsOut = goFS.CreateTextFile("..\data\46734115-2.csv")
Dim sLine
Do Until tsIn.AtEndOfStream
sLine = tsIn.ReadLine()
WScript.Echo "<", sLine
If "456," = Left(sLine, 4) Then
sLine = "789,""something else"""
End If
WScript.Echo ">", sLine
tsOut.WriteLine sLine
WScript.Echo
Loop
tsOut.Close
tsIn.Close
输出:
type ..\data\46734115.csv
123,"Active"
456,"Not-Active"
999000123,"Active"
cscript 46734115-3.vbs
< 123,"Active"
> 123,"Active"
< 456,"Not-Active"
> 789,"something else"
< 999000123,"Active"
> 999000123,"Active"
type ..\data\46734115-2.csv
123,"Active"
789,"something else"
999000123,"Active"
答案 1 :(得分:1)
根据Ekkehards的回答,这是ASP版本。 .csv文件需要与.asp脚本位于同一目录中。随意将积分奖励给Ekkehard
<%@LANGUAGE="VBSCRIPT"%>
<% option explicit %>
<%
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim tsIn : Set tsIn = goFS.OpenTextFile(Server.MapPath( "46734115.csv"))
Dim tsOut : Set tsOut = goFS.CreateTextFile(Server.MapPath("46734115-2.csv"))
Dim sLine
Do Until tsIn.AtEndOfStream
sLine = tsIn.ReadLine()
dim pos : pos = instr( sLine, """456"",")
Response.Write(pos)
if pos > 0 then
' to keep things simple, just replace the whole line
sLine = """456"",""Active"""
end if
tsOut.WriteLine sLine
' Just so there is something to see: print line to the browser window
Response.Write( sLine & "<br />")
Loop
tsOut.Close
tsIn.Close
%>