经典ASP - 如何preserve逗号在解析一个CSV文件 [英] Classic ASP - How to preserve commas while parsing a CSV file

查看:341
本文介绍了经典ASP - 如何preserve逗号在解析一个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景

4列,ID,PARENTID,类别,OrderNo和类别中可以有逗号,例如销售经理或HR,招聘,所以我将不得不处理,如果不能够知道的话大约有报价他们,因为他们使用不那么我需要处理与奇数逗号行,然后把这些行作为那些与类之间用逗号里面所以这是一个有点复杂的文件。

4 columns, ID, ParentID, Category, OrderNo and Category can have commas in it e.g "Sales, Manager" or "HR, Recruitment" so I would have to handle that without being able to know that the words have quotes around them as the files they use don't so I would need to handle rows with odd number of commas and then treat those rows as ones with categories with commas inside so it's a little more complicated.

推荐答案

我个人使用Microsoft文本驱动程序来解析CSV文件,使得与数据打交道容易得多。

I'd personally use the Microsoft Text Driver to parse CSV files, makes dealing with the data alot easier.

首先创建一个text.dsn文件,并在你的web应用程序的地方保存(在本例中,我会承担自己所在的C​​SV文件所在)

First create a text.dsn file and save it somewhere in your web app (in the example i'll assume its where the CSV file is located)

[ODBC]
DRIVER=Microsoft Text Driver (*.txt; *.csv)
UID=admin
UserCommitSync=Yes
Threads=3
SafeTransactions=0
PageTimeout=5
MaxScanRows=25
MaxBufferSize=512
ImplicitCommitSync=Yes
FIL=text
Extensions=txt,csv,tab,asc
DriverId=27

然后把它作为一个正常的数据库连接如:

Then treat it as a normal db connection eg:

strPath = server.mappath("/csv/")
sDSNFile = "text.dsn"
strCSVFile = "test.csv"

sDSN = "FileDSN=" & strPath & sDSNFile & ";DefaultDir=" & strPath & ";DBQ=" & strPath & ";"
Set Conn = CreateObject("ADODB.Connection")
Conn.Open sDSN
sql = "SELECT * FROM [" & strCSVFile & "]"

set rs = conn.execute(sql)

do until rs.eof
    id = rs("ID")
    ParentID = rs("ParentID")
    Category = rs("Category")
    OrderNo = rs("orderno")
    ' do something cool here
loop

这种方式,你可以拉说,所有的开发人员使用标准的SQL

This way you could pull say all developers out using standard sql

sql = "SELECT * FROM [" & strCSVFile & "] where Category='Developer'"

希望这有助于。

PS。如果你没有安装它,我觉得文字驱动程序包含为M的 icrosoft Access数据库引擎可再发行,但它已经有一段时间,可能是错误的:)

ps. If you don't have it installed, I think the text driver is included as part of Microsoft Access Database Engine redistributable, but it has been a while so may be wrong :)

这篇关于经典ASP - 如何preserve逗号在解析一个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆