简体   繁体   中英

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!

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM