How to convert string array to float array and substitute Double.NaN for non-numeric values?

笑着哭i 提交于 2020-01-25 03:41:16

问题


I'm writing a parser for CSV data, and am trying to determine how to handle records that are blank ("") or contain character data ("C"). The parser code I have below works great, but forces me to deal with the float conversions later. I'd like to be able to just make my string[][] a float[][], and handle the conversions when I parse the file, but I notice that it blows up with any non-numeric data. Ideally there would be no non-numeric or blank values, but they are unavoidable, and as such, have to be dealt with.

Can someone possibly recommend a concise approach to attempt to convert to Double, and then if it doesn't work, replace with Double.NaN instead? (Without sacrificing much performance if possible). Thank you.

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

//desiredFloatArray = [| 2.0; Double.NaN; Double.NaN |]


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 }

回答1:


Use this instead of the built-in float conversion:

let cvt s =
  let (ok,f) = System.Double.TryParse(s)
  if ok then f else nan


来源:https://stackoverflow.com/questions/2410408/how-to-convert-string-array-to-float-array-and-substitute-double-nan-for-non-num

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!