格式错误的CSV结尾

时间:2012-11-12 19:39:40

标签: vb.net parsing csv streamreader datareader

嘿所有我试图找出一种方法来纠正我的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()

1 个答案:

答案 0 :(得分:3)

您可以使用File.AppendAllText添加引号:

File.AppendAllText(filePath, """")