0

我在一个文件夹中有一堆 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()的代码中遇到异常,它表示无法使用第 5 行解析当前分隔符。我知道原因是因为双引号的存在。由于这是一个字符串数组,我认为我需要创建一个函数来处理数组中的每个项目并修剪掉双引号。但是,我不能这样做,因为即使在我可以处理字符串数组之前也会引发异常。

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

哈里

4

2 回答 2

1

StreamReader 可用于读取文本文件,只需看下面的示例即可满足您的需求:

请注意,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
于 2017-12-15T11:13:06.550 回答
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

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

哈里

于 2017-12-18T01:41:56.420 回答