mrs*_*eve 2 parsing haskell bytestring attoparsec
我有一个带有嵌套列表的文本文件(~300 MB大),类似于这个:
[[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94, 95], [4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 87, 92, 93, 94],[4, 9, 11, 28, 30, 45, 55, 58, 61, 62, 63, 69, 74, 76, 77, 82, 85, 87, 92, 93, 94, 95]]
Run Code Online (Sandbox Code Playgroud)
这是我的程序将文件读入haskell Integer列表:
import qualified Data.ByteString as ByteStr
main :: IO ()
-- HOW to do the same thing but using ByteStr.readFile for file access?
main = do fContents <- readFile filePath
let numList = readNums fContents
putStrLn (show nums)
Run Code Online (Sandbox Code Playgroud)
这适用于小文本文件,但我想用来ByteString快速读取文件.我发现readByteString 没有函数,你应该在attoparsec中编写自己的解析器,因为它支持解析ByteStrings.
我如何使用attoparsec解析嵌套列表?
数据似乎是JSON格式,因此您可以使用可以使用的Data.Aeson decode函数ByteString
import qualified Data.ByteString.Lazy as BL
import Data.Aeson
import Data.Maybe
main = do fContents <- BL.readFile filePath
let numList = decode fContents :: Maybe [[Int]]
putStrLn (show $ fromJust numList)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
424 次 |
| 最近记录: |