TextFieldParser中的ReadFields()抛出异常

问题描述 投票:0回答:2

我在一个文件夹中有一堆csv文件。这是一个示例:

Item    Value
Row1    Val1
Row2    Val2
Row3    Val3
Row4    Val4"
Row5    Val5

我根据该文件夹中所有csv文件中的可用信息编写了一个代码来绘制图表。这是我的按钮点击事件:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles generatePlot.Click

        Dim dirs As FileInfo() = GetFilesInDirectory("*.csv", True) 'Get all the csv file from result folder in descending order (creation date)
        Dim diNext As FileInfo

        Try
            For Each diNext In dirs.Reverse
                Using MyReader As New FileIO.TextFieldParser(diNext.FullName)
                    MyReader.TextFieldType = FileIO.FieldType.Delimited
                    MyReader.SetDelimiters(",")
                    Dim currentRow As String()
                    While Not MyReader.EndOfData
                        currentRow = MyReader.ReadFields()
                        processRow(diNext, currentRow)
                    End While
                End Using
            Next
        Catch ex As Exception
            MessageBox.Show(ErrorToString)
        End Try

        'Save chart as an image
        Chart1.SaveImage(imageSave, System.Drawing.Imaging.ImageFormat.Bmp)

    End Sub

如果你查看我的示例csv,Row4的值为Val4“。请注意其中的双引号。而且,我的代码中的currentRow = MyReader.ReadFields()中出现异常,表示无法使用当前分隔符。我知道原因是因为双引号的存在。由于这是一个字符串数组,我认为我需要创建一个函数来处理数组中的每个项目并修剪掉双引号。但是,我因为在我处理字符串数组之前抛出异常,所以无法做到这一点。

关于如何解决这个问题的任何想法?

arrays string vb.net csv textfieldparser
2个回答
1
投票

一个StreamReader can be used来读取文本文件,只需看看下面的例子来满足您的需求:

请注意,你不需要MemoryStreamWriter,只需要Reader

Public Sub ReadTest()
    Using MemoryStream As New IO.MemoryStream()
        Dim Writer As New IO.StreamWriter(MemoryStream) 'Writing on a memory stream to emulate a File
        Writer.WriteLine("Item,Value")
        Writer.WriteLine("Row1,Val1")
        Writer.WriteLine("Row2,Val2")
        Writer.WriteLine("Row3,Val3")
        Writer.WriteLine("Row4,Val4""")
        Writer.WriteLine("Row5,Val5")

        Writer.Flush()
        MemoryStream.Position = 0 'Reseting the MemoryStream to Begin Reading

        Dim Reader As New IO.StreamReader(MemoryStream) 'Reading from the Memory but can be changed into the File Path
        While Not Reader.EndOfStream
            Dim Line As String = Reader.ReadLine
            Dim Values() = Line.Split(",")
            'Values(0) will contain the First Item
            'Values(1) will contain the second Item

            Values(1).Replace("""", "") 'Remove the quote from the value string

        End While

    End Using
End Sub

0
投票

感谢@jmcilhinney和@AugustoQ提出的建议。

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles generatePlot.Click

        Dim dirs As FileInfo() = GetFilesInDirectory("*.csv", True) 'Get all the csv file from result folder in descending order (creation date)
        Dim diNext As FileInfo
        Dim currentRow As String()
        Try
            For Each diNext In dirs.Reverse
                For Each rawRows As String In File.ReadLines(diNext.FullName)
                    currentRow = processRawRow(rawRows)
                    processRow(diNext, currentRow)
                Next
            Next
        Catch ex As Exception
            MessageBox.Show(ErrorToString)
        End Try

        'Save chart as an image
        Chart1.SaveImage(imageSave, System.Drawing.Imaging.ImageFormat.Bmp)

    End Sub

我完全替换了TextFieldParser并使用了这个函数:

Private Function processRawRow(ByVal rawRows As String) As String()

        rawRows = rawRows.Replace("""", "").Trim()
        Dim processedList = rawRows.Split(",")
        Return processedList

    End Function

而且效果很好。感谢大家...

© www.soinside.com 2019 - 2024. All rights reserved.