编辑 CSV 记录

Edit a CSV Record

如何编辑 CSV 文件中的记录?

例如我有一个名为 "test.csv" 的 .csv 文件,里面是:

"123","Active"
"456","Not-Active"
"999000123","Active"

如何编辑 "456" 并将其从 Not-Active 更改为 Active

我能想到的唯一方法是:

  1. 打开 .csv 文件。也许将数据存储在一个字符串中?
  2. 搜索 "456","
  3. 获取"456","的行位置。如何做到这一点?
  4. 删除我们刚刚得到位置的行。如何做到这一点?
  5. 用我们想要的重新创建线条。如何做到这一点?
  6. 在行位置插入重新创建的数据。如何做到这一点?
  7. 保存 .csv 文件。

但是没有更简单的方法吗?

如果不是,我该如何执行第 4、5 和 6 步?

也许可以将其转换成数组之类的?但我不知道如何在 Classic ASP.

中执行此操作

script @abr mentioned的简化版本:

Dim goFS  : Set goFS  = CreateObject("Scripting.FileSystemObject")
Dim tsIn  : Set tsIn  = goFS.OpenTextFile("..\data734115.csv")
Dim tsOut : Set tsOut = goFS.CreateTextFile("..\data734115-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 ..\data734115.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 ..\data734115-2.csv
123,"Active"
789,"something else"
999000123,"Active"

根据 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
%>