对不起,非常随意的程序员,已经很多年没有编码了(主要是在VB6中) 我已经在Google上进行了搜索,并阅读了每个文件类的.net详细信息,看到了很多示例,但不知道如何获取修改后的输出以匹配源格式。
我已有文件:(这是2行文件:标题和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嵌入指示我已正确替换了数据。
我只需要输出与source相同的字符串即可。
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
我想念的是什么.. ??
答案 0 :(得分:0)
您需要将WriteLine移到For ... Next循环之外,以便每行只写一行。我使用一个列表来累积每一行中的字符串。该列表用于联接中。
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()
答案 1 :(得分:0)
玛丽,
那让我非常接近,现在我需要用双引号重新填充,我想我可以用谷歌搜索。
我确实需要将列表重新放置在暗淡的位置,并且列表清晰。.但是它使我准确地到达了我需要去的地方!
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