I am writing a parser for CSV data and trying to determine how to process records that are empty (") or contain character data (" C "). The parser code that I have below works fine, but makes me deal with converting the float later I would like my string [] [] to be float [] [] and handle the conversions when parsing the file, but I notice that it explodes with any non-numeric data. Ideally there should be no numerical or empty values, but they inevitable and, as such, should be considered.
Can someone recommend a brief approach to trying to convert to Double, and then if it doesn't work, replace Double.NaN instead? (Without sacrificing great efficiency, if possible). Thank.
let stringLine = [| "2.0"; "", "C"|]
let stringLine2Float = Array.map float stringLine
type csvData = { mutable RowNames: string[]; mutable ColNames: string[]; mutable Data: string[][] }
let csvParse (fileString: string) =
let colNames = ((fileLines fileString |> Seq.take 1 |> Seq.nth 0).Split(',')).[1..]
let lines = fileLines fileString |> Seq.skip 1 |> Array.ofSeq
let rowNames = Array.init lines.Length string;
let allData : string [][] = Array.zeroCreate rowNames.Length
for i in 0..rowNames.Length - 1 do
let fields = lines.[i].Split(',')
allData.[i] <- fields.[1..]
rowNames.[i] <- fields.[0]
{ RowNames = rowNames; ColNames = colNames; Data = allData }
source
share