【问题标题】:classic asp ignore comma within speech marks CSV经典asp忽略语音标记CSV中的逗号
【发布时间】:2013-11-08 09:49:09
【问题描述】:

我有一个如下所示的 CSV 文件
1,你好,英文
2,HELLO1,英文
3,HELLO2,英文
4,HELLO3,英文
5,HELLO4,英文
6,HELLO5,英文
7,HELLO6,英文
8,"HELLO7, HELLO7 ...",英文
9,HELLO7,英文
10,HELLO7,英文

我想逐行循环并使用逗号分隔的经典 asp 函数写入表。当存在语音标记时忽略这些语音标记中的逗号并获取字符串。

<%
dim csv_to_import,counter,line,fso,objFile
csv_to_import="uploads/testLang.csv"   
set fso = createobject("scripting.filesystemobject")
set objFile = fso.opentextfile(server.mappath(csv_to_import))
str_imported_data="<table cellpadding='3' cellspacing='1' border='1'>"

Do Until objFile.AtEndOfStream


  line = split(objFile.ReadLine,",")    
  str_imported_data=str_imported_data&"<tr>"
  total_records=ubound(line)

  for i=0 to total_records
    if i>0 then
     str_imported_data=str_imported_data&"<td>"&line(i)&"</td>"
    else
     str_imported_data=str_imported_data&"<th>"&line(i)&"</th>"
    end if
next
  str_imported_data=str_imported_data&"</tr>" & chr(13)


Loop
str_imported_data=str_imported_data&"<caption>Total Number of Records: "&total_records&"</caption></table>"
objFile.Close
response.Write str_imported_data
%>

【问题讨论】:

    标签: regex csv asp-classic vbscript


    【解决方案1】:

    Don't write your own CSV parser.

    您从“在, 上拆分它是要走的路,现在我完成了”开始。然后有人在您的数据中使用逗号,并且带有逗号的字符串用双引号引起来。你是个聪明人,所以你计算双引号的数量,如果它们是奇数,你知道你必须转义逗号,如果它们是偶数,你不必。然后你会得到一个包含转义双引号字符的 CSV 文件......

    但是等等!有一个解决方案。在您的文件中使用Database Connection

    会是这样,但您必须根据自己的情况进行调整:

    On Error Resume Next
    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adCmdText = &H0001
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordSet = CreateObject("ADODB.Recordset")
    
    strPathtoTextFile = server.mappath("uploads/")
    strFileName = "testLang.csv"
    
    objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
              "Data Source=" & strPathtoTextFile & ";" & _
              "Extended Properties=""text;HDR=NO;FMT=CSVDelimited"""
    
    objRecordset.Open "SELECT * FROM " & strFileName, _
              objConnection, adOpenStatic, adLockOptimistic, adCmdText
    
    Do Until objRecordset.EOF
        Wscript.Echo "Number: " & objRecordset.Fields.Item(1)
        Wscript.Echo "Greeting: " & objRecordset.Fields.Item(2)
        Wscript.Echo "Language: " & objRecordset.Fields.Item(3)   
        objRecordset.MoveNext
    Loop
    

    【讨论】:

      猜你喜欢
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 2022-01-08
      • 1970-01-01
      • 2018-02-15
      相关资源
      最近更新 更多