读取以散列分隔的CSV文件 [英] Read a CSV File Delimited with Hash

查看:90
本文介绍了读取以散列分隔的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从LDAP DB导出了CSV,并且必须使用Classic ASP页面读取数据.

I have a CSV export from an LDAP DB and I have to read data with a Classic ASP page.

CSV文件的格式类似于

The format of the CSV file is like

CRDLVR47E67L781V#1653#CORDIOLI#ELVIRA#658#elvira.cordioli@sender.at#SI

我可以逐行读取文件,并且必须手动拆分行. 如果将#值更改为逗号,则可以按列访问文件.我能否使asp页能够按列访问文件,以便获得保留#分隔符的单个值?

I can read the file line by line, and have to split the line manually. If I change the # value to a comma I can access the file by column. Can I make the asp page able to access the file by column, in order to obtain the single value keeping the # separator?

我的连接字符串是

Set oConn = Server.CreateObject("ADODB.connection") 
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=http://export/caselle.csv;Extended Properties='text;HDR=Yes;FMT=Delimited'"

我可以通过查询从CSV文件中读取行

and I can read line from the CSV file with the query

Set RS=Server.CreateObject("ADODB.recordset")
RS.open "SELECT * FROM utenti_aospbo.csv", oConn 

现在我只能读取rs.fields(0),它会输出整行,例如

now I can only read rs.fields(0), that output the entire line, like

CRDLVR47E67L781V#1653#CORDIOLI#ELVIRA#658#elvira.cordioli@sender.at#SI

我想要

response.write rs.fields(0) 'CRDLVR47E67L781V
response.write rs.fields(5) 'elvira.cordioli@sender.at

推荐答案

虽然我不能排除某些版本的OLEDB能执行HTTP或在连接字符串中接受FMT FORMAT ,我确定ADODB文本连接的数据源"属性需要为文件夹.

While I can't rule out that there is some version of OLEDB that does HTTP or accepts FMT and FORMAT in the connection string, I'm sure that the Data Source property of an ADODB Text connection needs to be a folder.

我不是尝试在连接字符串或注册表中指定全局分隔符,而是使用schema.ini文件以文件特定的方式描述元信息.

Instead of trying to specify a global separator in the connection string or the in the registry, I'd use a schema.ini file to describe the meta info in a file specific way.

全合一:

cscript 41224005.vbs
.\41224005.vbs
Option Explicit

Dim oFS  : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sF
For Each sF In Split(".\41224005.vbs ..\data\schema.ini ..\data\data1.txt")
    WScript.Echo sF
    WScript.Echo oFS.OpenTextFile(sF).ReadAll()
    WScript.Echo "---------------"
Next
Dim sDir : sDir    = oFS.GetAbsolutePathName("..\data\")
Dim sCS  : sCS     = Join(Array( _
                          "Provider=Microsoft.Jet.OLEDB.4.0" _
                        , "Data Source=" & sDir _
                        , "Extended Properties='" & Join(Array( _
                               "text" _
                           ), ";") & "'" _
                     ), ";")
Dim oDb  : Set oDb = CreateObject("ADODB.Connection")
oDb.Open sCS
'WScript.Echo oDb.ConnectionString
Dim oRS  : Set oRS = oDb.Execute("SELECT * FROM [data1.txt]")
WScript.Echo oRS.Fields(0).Value
WScript.Echo oRS.Fields(6).Value
oRS.Close
oDb.Close

---------------
..\data\schema.ini
[data1.txt]
FORMAT=Delimited(#)
ColNameHeader=False

---------------
..\data\data1.txt
CRDLVR47E67L781V#1653#CORDIOLI#ELVIRA#658#elvira.cordioli@sender.at#SI

---------------
CRDLVR47E67L781V
SI

这篇关于读取以散列分隔的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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