使用引号编写 CSV 以匹配源文件 - 使用 VB.net 和 TextfieldParser
Writing CSV with quotes to match source file - using VB.net, and TextfieldParser
抱歉,非常随意的程序员,很多年没有编码了..(主要是在 VB6 中)
我用谷歌搜索并阅读了每个文件的 .net 详细信息 class,看到了很多示例,但不知道如何修改输出以匹配源格式。
我有现有文件:(这是 2 行文件:Header 和 1 个细节)
"BATCHNBR","JOURNALID","TRANSNBR","DESCOMP","ROUTE","ACCTID","COMPANYID","TRANSAMT","TRANSQTY","SCURNDEC","SCURNAMT","HCURNCODE","RATETYPE","SCURNCODE","RATEDATE","CONVRATE","RATESPREAD","DATEMTCHCD","RATEOPER","TRANSDESC","TRANSREF","TRANSDATE","SRCELDGR","SRCETYPE","COMMENT","VALUES","PROCESSCMD"
"000004","00001","0000000020","",0,"60000000000000000ZZZZZ","GMN",817.31,0,"2",817.31,"USD","SP","USD",20180511,1,0,"1","1","100123","10444-Kampwerth, Brooke E",20180511,"UP","SC","",0,0
我已经完成了子程序,但它在它自己的行上输出每个字段,并且没有双引号。 TextFieldParser 没有写入方法。 msgbox 嵌入表明我已正确替换数据,
我正在截断某个字段(如果需要),并且 msgbox 嵌入表明我已正确替换数据。
我只需要输出与源相同的字符串。
Private Sub cmdProcess_Click(sender As Object, e As EventArgs) Handles cmdProcess.Click
Using sw = New IO.StreamWriter(NewFile)
Using MyReader As New FileIO.TextFieldParser(oFile)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
'MyReader.HasFieldsEnclosedInQuotes
'Dim Expression As String
Dim Replacement As String
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
If Strings.Right(currentField, 3) = "ZZZ" Then
'MsgBox(MyReader.LineNumber & " " & currentField)
Replacement = Strings.Left(currentField, 6)
'MsgBox(MyReader.LineNumber & " " & Replacement)
currentField = Replacement
End If
sw.WriteLine(String.Join(",", currentField))
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End Using
MsgBox("Done")
Dispose()
Application.Exit()
End Sub
如果我用 sw.writeline 中的 currentrow 替换 currentfield,它会输出同一行 27 次(源代码中有 27 行),然后执行下一行。每个字段周围没有双引号。
当前输出:(单行)
BATCHNBR
JOURNALID
TRANSNBR
DESCOMP
ROUTE
我错过了什么.. ??
您需要将 WriteLine 移到 For...Next 循环之外,这样它每行只写一行。我用一个列表来累积每一行中的字符串。该列表用于 Join。
Dim lstRow as New List(Of String)
For Each currentField In currentRow
If Strings.Right(currentField, 3) = "ZZZ" Then
'MsgBox(MyReader.LineNumber & " " & currentField)
Replacement = Strings.Left(currentField, 6)
'MsgBox(MyReader.LineNumber & " " & Replacement)
currentField = Replacement
End If
lstRow.Add(currentField)
Next
sw.WriteLine(String.Join(",", lstRow))
lstRow.Clear()
玛丽,
这让我非常接近,现在我需要用双引号重新填充,我想我可以 google。
我确实需要将列表重新定位为暗淡的,并且列表清晰..但它让我准确地找到了我需要去的地方,谢谢!!!
Dim Replacement As String
Dim lstRow As New List(Of String)
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
If Strings.Right(currentField, 3) = "ZZZ" Then
'MsgBox(MyReader.LineNumber & " " & currentField)
Replacement = Strings.Left(currentField, 6)
'MsgBox(MyReader.LineNumber & " " & Replacement)
currentField = Replacement
End If
lstRow.Add(currentField)
Next
sw.WriteLine(String.Join(",", lstRow))
lstRow.Clear()
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
抱歉,非常随意的程序员,很多年没有编码了..(主要是在 VB6 中) 我用谷歌搜索并阅读了每个文件的 .net 详细信息 class,看到了很多示例,但不知道如何修改输出以匹配源格式。
我有现有文件:(这是 2 行文件:Header 和 1 个细节)
"BATCHNBR","JOURNALID","TRANSNBR","DESCOMP","ROUTE","ACCTID","COMPANYID","TRANSAMT","TRANSQTY","SCURNDEC","SCURNAMT","HCURNCODE","RATETYPE","SCURNCODE","RATEDATE","CONVRATE","RATESPREAD","DATEMTCHCD","RATEOPER","TRANSDESC","TRANSREF","TRANSDATE","SRCELDGR","SRCETYPE","COMMENT","VALUES","PROCESSCMD"
"000004","00001","0000000020","",0,"60000000000000000ZZZZZ","GMN",817.31,0,"2",817.31,"USD","SP","USD",20180511,1,0,"1","1","100123","10444-Kampwerth, Brooke E",20180511,"UP","SC","",0,0
我已经完成了子程序,但它在它自己的行上输出每个字段,并且没有双引号。 TextFieldParser 没有写入方法。 msgbox 嵌入表明我已正确替换数据,
我正在截断某个字段(如果需要),并且 msgbox 嵌入表明我已正确替换数据。
我只需要输出与源相同的字符串。
Private Sub cmdProcess_Click(sender As Object, e As EventArgs) Handles cmdProcess.Click
Using sw = New IO.StreamWriter(NewFile)
Using MyReader As New FileIO.TextFieldParser(oFile)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
'MyReader.HasFieldsEnclosedInQuotes
'Dim Expression As String
Dim Replacement As String
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
If Strings.Right(currentField, 3) = "ZZZ" Then
'MsgBox(MyReader.LineNumber & " " & currentField)
Replacement = Strings.Left(currentField, 6)
'MsgBox(MyReader.LineNumber & " " & Replacement)
currentField = Replacement
End If
sw.WriteLine(String.Join(",", currentField))
Next
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
End Using
MsgBox("Done")
Dispose()
Application.Exit()
End Sub
如果我用 sw.writeline 中的 currentrow 替换 currentfield,它会输出同一行 27 次(源代码中有 27 行),然后执行下一行。每个字段周围没有双引号。
当前输出:(单行)
BATCHNBR
JOURNALID
TRANSNBR
DESCOMP
ROUTE
我错过了什么.. ??
您需要将 WriteLine 移到 For...Next 循环之外,这样它每行只写一行。我用一个列表来累积每一行中的字符串。该列表用于 Join。
Dim lstRow as New List(Of String)
For Each currentField In currentRow
If Strings.Right(currentField, 3) = "ZZZ" Then
'MsgBox(MyReader.LineNumber & " " & currentField)
Replacement = Strings.Left(currentField, 6)
'MsgBox(MyReader.LineNumber & " " & Replacement)
currentField = Replacement
End If
lstRow.Add(currentField)
Next
sw.WriteLine(String.Join(",", lstRow))
lstRow.Clear()
玛丽,
这让我非常接近,现在我需要用双引号重新填充,我想我可以 google。
我确实需要将列表重新定位为暗淡的,并且列表清晰..但它让我准确地找到了我需要去的地方,谢谢!!!
Dim Replacement As String
Dim lstRow As New List(Of String)
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
If Strings.Right(currentField, 3) = "ZZZ" Then
'MsgBox(MyReader.LineNumber & " " & currentField)
Replacement = Strings.Left(currentField, 6)
'MsgBox(MyReader.LineNumber & " " & Replacement)
currentField = Replacement
End If
lstRow.Add(currentField)
Next
sw.WriteLine(String.Join(",", lstRow))
lstRow.Clear()
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try