CSV解析无法识别换行符(ASP)

时间:2014-04-15 00:29:36

标签: csv vbscript asp-classic

我正在使用VBScript来解析CSV数据并将其显示在表格上。我已经设置了CSV,当我在Sublime Text中打开它时,标题和内容在不同的行上(除了标题之外只有一行内容)。它在Sublime Text中显示两行。

标题在行的末尾没有逗号,因此它不会将其分解,只是基本上将第二行的第一个条目附加到标题行的最后一个条目,因为它在技术上没有划分用逗号。

我正在使用的代码是:

<html>
<head>
</head>
<body>
<%@language="vbscript"%>
<table border="1">
<%
dim csv_to_read, fso, act
csv_to_read="sample.csv"
set fso = createobject("scripting.filesystemobject")
set act = fso.opentextfile(server.mappath(csv_to_read))
'Read the first line of the csv, typically these are colum headings
Response.Write "<tr><th>" & replace(act.readline,",","</th><th>") & "</th></tr>" & vbCrLf
'Read the rest of the csv
Response.Write "<tr><td>" & replace(replace(act.readall,vbCrLf,"</td></tr>"&vbCrLf&"<tr><td>"),",","</td><td>") & "</td></tr>"
%>
<caption>Total Number of Records: <%=act.Line-1%></caption>
</table>
</body>
</html>

为什么不识别换行符?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

认为您的问题是由于文件没有VOLCrLf的EOL标记(但可能是crLf)。证据:

s = "1,2,3" & vbLf & "4,5,6"
h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
WScript.Echo "vbLf"
WScript.Echo h
s = "1,2,3" & vbCrLf & "4,5,6"
h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
WScript.Echo "vbCrLf"
WScript.Echo h

输出:

cscript 23072652.vbs
vbLf
<tr><td>1</td><td>2</td><td>3
4</td><td>5</td><td>6</td></tr>
vbCrLf
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>

(我希望输出将向循环拥护者证明双重替换产生有效的HTML)

更新评论:

此版本的脚本:

s = "1,2,3" & vbCrLf & "4,5,6"
WScript.Echo ".ReadAll() (faked):"
WScript.Echo s

' h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
' step by step

h = replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>")
WScript.Echo "step 1 - EOL handling:"
WScript.Echo h
h = replace(h,",","</td><td>")
WScript.Echo "step 2 - comma handling:"
WScript.Echo h
h = "<tr><td>" & h & "</td></tr>"
WScript.Echo "step 3 - head & tail I:"
WScript.Echo h
h = "<!DOCTYPE html><html><head><title>replace demo</title></head><body><table>" & vbCrLf & h & vbCrLf & "</table></body></html>"
WScript.Echo "step 4 - head & tail II:"
WScript.Echo h

输出:

cscript 23072652.vbs
.ReadAll() (faked):
1,2,3
4,5,6
step 1 - EOL handling:
1,2,3</td></tr>
<tr><td>4,5,6
step 2 - comma handling:
1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6
step 3 - head & tail I:
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
step 4 - head & tail II:
<!DOCTYPE html><html><head><title>replace demo</title></head><body><table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
</table></body></html>

应该使双重替换的工作方式可见,并且所有部分都很重要。

答案 1 :(得分:1)

只需快速浏览一下,您就会发现格式错误的HTML。

Response.Write "<tr><td>" & replace(replace(act.readall,vbCrLf,"</td></tr>"&vbCrLf&"<tr><td>"),",","</td><td>") & "</td></tr>"

我读取内部替换为用关闭表格单元格/表格行替换每个回车符 外部替换是用关闭单元格/开放单元格替换逗号 然后,您将附加另一个关闭单元格/结束行

在我看来,循环遍历行,无论是否提高性能,都更容易阅读和调试。如果这适用于您的情况,请考虑以下内容:

Reading csv file in classic asp. Problem: column values are truncated up to 300 characters

How to read CSV files line by line in VBScript