Quickly read text data into an array

I find it difficult to read from a text file into an array of floats using F #. Text files have many other data types, so I can't use the CSV parser, but I'm sure there should be a simple function for this. In Python, I just scrolled all the lines of interest and added them to an existing array using something like this: Reading a file line into an array (in the Putin way)

arrays = []
i = 1
for line in open(your_file):
    if i > startOfNumericDataIndex
        new_array = np.array((array.float(i) for i in line.split(' '))) 
        arrays.append(new_array)
    i++

I am trying to avoid loops according to F # style, but the following attempts do not work:

let lines = System.IO.File.ReadLines(path) //Collection
let linesStringArray = lines |> Seq.toArray // String array
let linesFloatArray = linesStringArray |> Array.map (fun x -> float x)

I get rror FS0001: it is expected that this expression will be of type list, but there is a type string here, but for many years I have turned it into lists of strings and other types.

:   float Double.NaN ?: , :

let stringLine = [| "2.0"; "3.0"; "2.0"|]    
let stringLine2Float = Array.map float stringLine

'' '' 'string' '.

+4
1

Seq.collect, :

let lines = System.IO.File.ReadLines(path) //Collection
let linesFloatArray = linesStringArray 
|> Seq.skip startOfNumericDataIndex
|> Seq.collect (fun line -> line.Split(' '))
|> Seq.map (fun x -> float.Parse x)
|> Array.ofSeq

, :

let lines = System.IO.File.ReadLines(path) //Collection
let linesFloatArray = linesStringArray 
|> Seq.skip startOfNumericDataIndex
|> Seq.map (fun line -> line.Split(' ') |> Array.map (fun x -> float.Parse(x)))
|> Array.ofSeq
+2

Source: https://habr.com/ru/post/1568409/


All Articles