我正在尝试将变量中的字符串附加到CSV中的行尾。但是,换行符正在被字符串替换。因此,如果原始行是:
fieldcharacters1,fieldcharacters string从一行开始
并在换行后继续,fieldcharacters3
(空白行是换行符)
我希望将“fieldcharacter4”(不带引号)附加到该行的末尾,应该显示如下:
fieldcharacters1,fieldcharacters string从一行开始
并在换行后继续,fieldcharacters3,fieldcharacter4
但是它最终会像这样结束:
fieldcharacters1,fieldcharacters string从一个开始 line,fieldcharacter4,fieldcharacter4并在一行后继续 饲料,fieldcharacters3,fieldcharacter4
这是我的代码。我还尝试了使用objCurrentFile.AtEndOfStream
的Do Until循环,但它也没有用。
Set tsOut = objFSO.CreateTextFile(strCSV, FOR_WRITING)
strLine = objCurrentFile.ReadAll
objCurrentFile.close
strInsertText = ",testing123123" & chr(013)
WScript.Echo "Text to be appended= " & strInsertText
strNewText = Replace(strLine, Chr(013), strInsertText )
tsOut.Write strNewText
答案 0 :(得分:1)
有三种行尾标记:
从文件读取时,.ReadLine()将删除/ n或/ r / n,但不删除/ r(参见here);但是,.ReadLine()将返回文件的所有内容。
一个简单的问题模型:你有一个.CSV包含嵌入\ r \ n和\ r \ n为eol的字段;当'试图通过替换将变量中的字符串附加到行的末尾'时,嵌入的\ r \ n也会受到影响:
>> sLine = "field 1,... one line\rand ...field 3\r\n"
>> WScript.Echo Replace(sLine, "\r", "[,append]")
>>
field 1,... one line[,append]and ...field 3[,append]\n
您可以放弃替换\ r \ n:
>> WScript.Echo Replace(sLine, "\r\n", "[,append]")
>>
field 1,... one line\rand ...field 3[,append]
并处理文件中最终替换尾随\ r \ n的问题。如果你可以遍历.CSV行,则追加/连接(而不是使用.Replace)额外的数据和正确的eol标记(可能通过.WriteLine)。
如果eol-和嵌入式标记相同,第一种方法将失败。在这种情况下(可能一般情况下)使用ADO Text驱动程序来处理.CSV是一个更好的选择。
(顺便说一句:阅读docs了解原因
objFSO.CreateTextFile(strCSV, FOR_WRITING)
错了)
更新评论:
根据您的评论,我得出结论,您的.CSV使用\ r \ n用于eol 和嵌入(段落?)标记。所以替换不起作用:
>> sLine = "field 1,... one line\r\nand ...field 3\r\n"
>> WScript.Echo Replace(sLine, "\r\n", "[,append]")
>>
field 1,... one line[,append]and ...field 3[,append]
使用.ReadLine()时,您还必须在嵌入式\ r \ n中终止“短线”终止。所以你的选择是:
一个粗略的想法:
在短暂查看样本数据后,我可以确认\ r \ n用于某些引用字段中的eol 和段落。第一次测试表明ADO文本驱动程序可以处理它。
但是因为第一个字段包含引用的IP地址,你可以区分eol(\ r \ n后跟“(或”IP地址“))和段落(\ r \ n 不是)通过“(或”IP地址“))。如果没有“在'paragraphed'字段中,在.ReadAll上的一个(不是那么)简单的替换\ r \ n”将解决问题;否则需要一个参考IP地址的RegExp。
演示代码:
Option Explicit
Dim sCsv : sCsv = Replace(Join(Array( _
"'1.2.3.4','line1\r\nline2',1,'whatever'" _
, "'5.6.7.8','linea\r\nlineb',2,'pipapopu'" _
, "" _
), "eol"), "'", """")
WScript.Echo "faked .CSV (embedded \r\r, EOL \r\n)"
WScript.Echo Replace(sCsv, "eol", "\r\n" & vbCrLf) ' for display
sCsv = Replace(Replace(sCsv, "eol", vbCrLf), "\r\n", vbCrLf) ' 'real data'
WScript.Echo "raw display of .CSV"
WScript.Echo sCsv
If False Then
Dim re : Set re = New RegExp
re.Global = True
re.Pattern = "(\r\n""\d+(\.\d+){3}"")" ' EOL followed by "IP Address" of next line
' dirty hack to append to last line
sCsv = sCsv & """0.0.0.0"""
sCsv = re.Replace(sCsv, ",""append""$1")
' remove trailing junk (\r\n"IP Address")
sCsv = Left(sCsv, Len(sCsv) - 11)
Else
' dirty hack to append to last line
sCsv = sCsv & """"
sCsv = Replace(sCsv, vbCrLf & """", ",""append""" & vbCrLf & """")
' remove trailing junk (\r\n")
sCsv = Left(sCsv, Len(sCsv) - 3)
End If
WScript.Echo "raw display of .CSV after appending"
WScript.Echo sCsv
输出(两种方法):
cscript 24673618.vbs
faked .CSV (embedded \r\r, EOL \r\n)
"1.2.3.4","line1\r\nline2",1,"whatever"\r\n
"5.6.7.8","linea\r\nlineb",2,"pipapopu"\r\n
raw display of .CSV
"1.2.3.4","line1
line2",1,"whatever"
"5.6.7.8","linea
lineb",2,"pipapopu"
raw display of .CSV after appending
"1.2.3.4","line1
line2",1,"whatever","append"
"5.6.7.8","linea
lineb",2,"pipapopu","append"