将VB.Net CSV文件读入列表时出现问题,缺少一半的值 [英] Issue with Reading VB.Net CSV Files into List, Missing Half of the Values

查看:58
本文介绍了将VB.Net CSV文件读入列表时出现问题,缺少一半的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图读取CSV文件,该文件包含数字数据,该数字数据由文件中19列中的每列1000行组成.在读取单个文件中的每一列并将值添加到小数列表中时,我已经成功完成了当前代码,但是我收到一个有趣的用户错误,似乎无法弄清楚.输出列表仅会添加CSV文件中一半的值,因此将第二个for循环中指定的行范围减半似乎会将输出值的数量减半(即,for循环中指定的1000行会导致结果中包含500个值列表,则500行将在结果列表中产生250个值).从具有文件路径作为由filePath表示的参数的函数中调用代码:

I have been attempting to read in CSV files containing numeric data consisting of 1000 rows for each of the 19 columns within the files. I have been succesful with my current code in reading each column in an individual file and adding the values to a list of decimals but I am receiving an interesting user error that I cannot seem to figure out. The output list only adds half of the values from the CSV file such that halving the row range specified in the second for loop seems to halve the amount of outputted values (ie. 1000 rows specified in the for loop results in 500 values in the outcome list, 500 rows results in 250 values in the outcome list). The code is called from a function with the file path as an argument represented by filePath:

'''

    If My.Computer.FileSystem.FileExists(filePath) = False Then 'What to do if the file does not exist at the specified path
        Throw New Exception("File Not Found: " & filePath) 'Error message if file does not exist
    ElseIf My.Computer.FileSystem.FileExists(filePath) = True Then 'What to do if the file does exist at the specified path
        Dim fileData = IO.File.ReadAllLines(filePath)
        Dim fileColumnCount = (fileData.First.Split(","c).Length) - 1
        Dim fileRowCount = fileData.Length
        Dim column As Integer
        Dim row As Integer
        Dim currentColumn As New List(Of Decimal)
        column = 0
        For column = 0 To fileColumnCount
            row = 0
            currentColumn.Clear()
            For row = 0 To (fileRowCount - 1)
                Dim placeholder As String = fileData(row)
                Dim currentLine() As String = placeholder.Split(",")
                Dim lineList As List(Of String) = currentLine.ToList()
                Dim value_dec As Decimal = Convert.ToDecimal(lineList(column))
                currentColumn.Add(value_dec)
                row += 1
            Next
            column += 1
        Next
    End If
End Function

'''

当在重新启动第一个for循环之前使用断点运行程序之后检查currentColumn(CSV文件中单个列的输出列表)时,仅检查第一列,只有值的前一半被添加.即使row等于1000表示已经解析了每个行的值,但我始终会丢失一半的数据.有什么想法或建议吗?谢谢.

When I check currentColumn (the output list for an individual column within the CSV file) after running the program with a breakpoint prior to restarting the first for loop so that only the first column is checked, only the first half of the values are added. Even though row is equal to 1000 indicating that each row value has been parsed, I am always missing half of the data. Any thoughts or advice? Thank you.

经过更多测试后,输出数据集中似乎没有包含均匀分布的值.仍然无法解决问题.

After some more testing, it seems that evenly spaced values are not being included in the output dataset. Still no luck fixing the issue.

推荐答案

您无需在循环中将行加1.

You don't need to make row add 1 in the loop.

            For row = 0 To (fileRowCount - 1)
                ...
                row += 1
            Next

删除行+ = 1 :

            For row = 0 To (fileRowCount - 1)
                Dim placeholder As String = fileData(row)
                Dim currentLine() As String = placeholder.Split(",")
                Dim lineList As List(Of String) = currentLine.ToList()
                Dim value_dec As Decimal = Convert.ToDecimal(lineList(column))
                currentColumn.Add(value_dec)
            Next

这篇关于将VB.Net CSV文件读入列表时出现问题,缺少一半的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆