打开,编辑和重新保存CSV文件

时间:2013-11-12 18:05:44

标签: csv asp-classic vbscript

我有一个类似.CSV的文件,我想做两件事之一。

文件中的数据如下所示:

"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER","DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT","RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE",1.00,1.00,"E","004ACS","02812","365","-","-","0",0.00,"-","JOHN S  ",11/4/2013,"16:18:49E","IC","N",3257.00,"IRVING HOULTON","HOULTON","ME",3.95,3.95,121.57,480.08,0.00,0.00,0.00,0.00,480.08,1.50,0.00,481.58
"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER","DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT","RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE",1.00,2.00,"E","014ACI","976234","430","-","-","0",0.00,"-","STACY    ",11/4/2013,"00:21:16E","F","Y",8796.00,"PILOT 405","TIFTON","GA",3.77,3.77,172.65,650.73,0.00,0.00,0.00,0.00,650.73,1.50,0.00,652.23
"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER","DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT","RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE",1.00,1.00,"E","004ACS","02812","365","-","-","0",0.00,"-","JOHN S  ",11/4/2013,"16:18:49E","IC","N",3257.00,"IRVING HOULTON","HOULTON","ME",3.95,3.95,121.57,480.08,0.00,0.00,0.00,0.00,480.08,1.50,0.00,481.58
"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER","DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT","RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE",1.00,2.00,"E","014ACI","976234","430","-","-","0",0.00,"-","STACY    ",11/4/2013,"00:21:16E","F","Y",8796.00,"PILOT 405","TIFTON","GA",3.77,3.77,172.65,650.73,0.00,0.00,0.00,0.00,650.73,1.50,0.00,652.23

每一行都以此重复文字开头:"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER","DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT","RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE",

我想做的是......

选项1:

  1. 将重复的文字放在字符串中
  2. 删除字符串中的最后一个字符
  3. 使用no中的字符串从文件中删除所有其他重复文本。 1
  4. 保存.CSV文件
  5. 选项2:

    1. 将重复的文字放在字符串中
    2. 删除字符串中的最后一个字符
    3. 每个循环/每行我都会跳到第305列
    4. 保存.CSV文件
    5. 如何在classic-ASP中执行此操作?

1 个答案:

答案 0 :(得分:3)

.ReadLine()/ 2文件方法:

  Dim tsIn  : Set tsIn  = goFS.OpenTextFile("..\data\19936645.csv")
  Dim tsOut : Set tsOut = goFS.CreateTextFile("..\data\19936645-2.csv")
  Dim sFrs  : sFrs      = tsIn.ReadLine()
  Dim sLast : sLast     = """INV_BALANCE"","
  Dim nCutP : nCutP     = Instr(sFrs, sLast) + Len(sLast)
  Dim sHead : sHead     = Left(sFrs, nCutP - 2)
  Dim sLine
  tsOut.WriteLine sHead
  tsOut.WriteLine Mid(sFrs, nCutP)
  Do Until tsIn.AtEndOfStream
     sLine = tsIn.ReadLine()
     If 1 = Instr(sLine, sHead) Then
        tsOut.WriteLine Mid(sLine, nCutP)
     Else
        WScript.Echo "Bingo!"
     End If
  Loop
  tsOut.Close
  tsIn.Close

用列标题创建一个新的.csv。