0

嘿,我正试图找出一种方法来纠正我的 CSV 文件中的错误,然后再出现MalformedLineException 错误

我的代码是这样的:

Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\temp.csv")
        myreader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        myreader.Delimiters = New String() {",", "\n"}
        myreader.HasFieldsEnclosedInQuotes = True 'Added

        While Not myreader.EndOfData
            Try
                currentrow = myreader.ReadFields()

错误出现在currentrow = myreader.ReadFields()上。这是由于 CSV 的最后一行中没有结束引号引起的:

"xx.xxx.xxx.xx","2012-05-15 13:15:54","Bob Barker","bbarker@priceisright.com","

它应该是:

"xx.xxx.xxx.xx","2012-05-15 13:15:54","Bob Barker","bbarker@priceisright.com",""

在它到达currentrow = myreader.ReadFields()行之前,我该如何纠正它?

4

1 回答 1

3

您可以使用File.AppendAllText添加报价:

File.AppendAllText(filePath, """")
于 2012-11-12T19:52:42.257 回答