Home > Net >  Import txt file data and store it in Multidimensioanl array in Vb.net
Import txt file data and store it in Multidimensioanl array in Vb.net

Time:09-09

Sorry if the problem is so basic, I'm a bit used to python not VB.net

I'm trying to read text file data (numbers) and store it in array/list

    # Sample of text

        1.30e 03,1.30e 03,1.30e 03
        5.4600e 02,2.7700e 02,2.8000e 02
    
# PS: I can control the output of the numbers to have delimiter = ',' or space between numbers, whatever is easier to import

I wrote the following code to read string data and store it. yet, I don't know how to have a multidimensional array (2D or 3D) instead of 1D string (e.g. for the text above, it would be 2x3 array)

    ' Import Data
    Comp_path = FinalPath & "Components_colors.txt"
    reader = New StreamReader(Comp_path)
    Dim W As String = ""
    Dim wArray(10) As String
    Dim i As Integer = 0

    Do Until reader.Peek = -1
        W = reader.ReadLine()
        wArray(i) = W
        i  = 1

    Loop

Moreover, I don't know the length of the text file, so I can't determine the length of the array like I did in the code above for the string wArray

CodePudding user response:

For a file like this, you should turn to NuGet for a dedicated CSV parser. There are parsers built into .NET you could also use, but pulling one off of NuGet will also let you parse the values directly into something other than a string.

But if you really don't want to do that you can start with this (assuming Option Infer):

Public Function ImportData(filePath As String) As IEnumerable(Of Double())
    Dim lines = File.ReadLines(filePath)
    Return lines.Select(Function(line) line.Split(",").Select(AddressOf Double.Parse).ToArray())
End Function

And use it like this:

Comp_path = FinalPath & "Components_colors.txt"
Dim result = ImportData(Comp_path)

Note this code doesn't actually do any meaningful work yet. It doesn't even read the file. What it does is give you an object (result) that you can use with a For Each loop or linq operations. It will read the file in a just-in-time way, parsing out the data for each line as it goes. If you want an array (or List, which you should use in .Net more often), you can append a ToList() call to the end:

Comp_path = FinalPath & "Components_colors.txt"
Dim result = ImportData(Comp_path).ToList()

But you should try to avoid doing that it. It's much less efficient in terms of memory use. The first sample will only ever need to keep one line of the file in memory at a time. Adding ToArray() or ToList() needs to load the entire file.


Some more notes:

  • Many newer dynamic platforms like Python don't actually use real arrays in the formal computer science sense (fixed block of contiguous memory). Rather, they use collections, and just call them arrays. .Net has collections, too, but when you declare an array, you get an array. This has nice benefits for performance, but if you don't know you want that or how to take advantage of it you're probably better off asking for a generic List most of the time instead.
  • Thanks to cultural/internationalization issues, parsing numeric (or date) values to string and back again is much slower and more error-prone than you've believed in the past, especially coming from a dynamic platform. It is slow on these other platforms, too, but they want you to pretend it isn't. The first introduction to a strongly-typed platform like .Net can feel stifling in this area, but once you understand the performance and and reliability benefits, you won't want to go back.
  • In strongly-typed platforms it is very important to understand the data types you are working with at every level of an expression. Otherwise, building and reading statements like the Return line in my answer will be way more difficult and frustrating than it needs to be.
  • Related