我有一个VB .Net winforms应用程序,它解析CSV文件并将解析后的行添加到表结构中。
Dim table As New List(Of String())
Using afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(fileName)
afile.HasFieldsEnclosedInQuotes = True
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
Do While Not afile.EndOfData
Try
Dim record As String() = afile.ReadFields()
table.Add(record)
Catch ex As FileIO.MalformedLineException
If ex.Message.Contains("cannot be parsed using the current Delimiters") Then
Dim textToReplace As String = "(?<!,)""(?!,)"
Dim fixedString As String = Regex.Replace(afile.ErrorLine, textToReplace, "'")
End If
End Try
Loop
End Using
问题是,有时CSV格式错误,并且在字符串中间包含双引号。如下所示:
123,456,"abcde",100
789,098,"fg"hij",101 <--- problem line
765,432,"klmno",102
这就是为什么我有错误处理程序来捕获错误并通过将其转换为单引号来修复有问题的双引号。问题是,当我解决问题时,我不知道该线的其余部分是什么。因为线路有问题,所以没有任何内容被读入。我可以通过Error对象访问有问题的字段,但是有关它的信息。不幸的是,即使出现错误,TextFieldParser也会移动到下一行。有办法退回一行吗?