Created
June 29, 2022 14:33
-
-
Save vanaur/694984b180554552507876d3b7d1fcc9 to your computer and use it in GitHub Desktop.
Parse a file of spaced columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "nuget: FParsec, 1.1.1" | |
open FParsec | |
let ws = skipMany (pchar ' ' <|> pchar '\t') | |
let ws1 = skipMany1 (pchar ' ' <|> pchar '\t') | |
let parseComment = | |
pstring "#" >>. skipRestOfLine true >>% [] | |
let parseNumber = pfloat <|> (pint32 |>> float) | |
let parseTable = | |
parse { | |
do! spaces | |
let! y = sepBy parseNumber ws1 | |
let nd = y.Length - 1 | |
let parseRow = | |
parse { | |
let! x = parseNumber | |
let! xs = parray nd (ws >>. parseNumber) |>> Array.toList | |
do! skipMany1 newline | |
return x :: xs | |
} | |
do! skipMany1 newline | |
let! ys = | |
many (attempt parseRow <|> parseComment) | |
|>> List.filter (not << List.isEmpty) | |
let result = y :: ys | |
return List.transpose result | |
} | |
let runParser = runParserOnString parseTable () "" | |
printfn "%A" (runParser (System.IO.File.ReadAllText "data.dat")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45.1 2.78 2.78 2.78 2.78 2.78 | |
40.0 2.70 2.70 2.70 2.70 2.70 | |
30.0 2.54 2.54 2.54 2.54 2.54 | |
20.0 2.35 2.35 2.35 2.35 2.35 | |
10.0 2.13 2.13 2.13 2.13 2.13 | |
5.02 2.00 2.00 2.00 2.00 2.00 | |
3.01 1.93 1.93 1.93 1.93 1.93 | |
0.48 1.80 1.80 1.80 1.80 1.80 | |
0.28 1.77 1.77 1.77 1.77 1.77 | |
0.11 1.72 1.72 1.72 1.72 1.72 | |
0.05 1.68 1.68 1.68 1.68 1.68 | |
0.24 1.64 1.64 1.64 1.64 1.64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment