我有一个带有嵌套列表的文本文件(〜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]]
这是将文件读入haskellInteger
列表的程序:
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)
这适用于小型文本文件,但我想用来ByteString
快速读取文件。我发现read
ByteString没有功能,相反,您应该在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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句