Ayto Maximo
Ayto Maximo

Reputation: 145

F# CSV - for each row create an array from columns data

I have a CSV file, where the fst column is a title and next 700+ columns are some int data.

Title D1 D2 D3 D4 .. D700
Name1  0  1  7  5     48 

I try to use CsvProvider to read the file and then convert data to my custom type

type DigitRecord = { Title:string; Digits:int[] }

The problem is I don't know how to put all column data (except the first one with a title) into a int[] array.

 let dataRecords = 
    CSV.Rows 
    |> Seq.map (fun record -> {Title = record.Title; Digits = ???})

I want to get a record with Title=Name1 and Digits=[|0,1,7,5...48|]

I'm newbie in F#, I'd be grateful for any help!

Upvotes: 5

Views: 483

Answers (1)

FoggyFinder
FoggyFinder

Reputation: 2220

I think the easiest way is to use CsvParser like this:

let readData (path : string) seps =
    CsvFile.Load(path, seps).Rows
    |> Seq.map
        (fun row -> row.Columns.[0], row.Columns |> Array.skip 1 |> Array.map int)
    |> Seq.map
        (fun (title, digits) -> {Title = title; Digits = digits})

Upvotes: 2

Related Questions