【发布时间】:2013-11-11 04:00:25
【问题描述】:
我有一个带有嵌套列表的文本文件(约 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]]
这是我将文件读入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)
这适用于小文本文件,但我想使用ByteString 快速读取文件。我发现 ByteString 没有read 函数,你应该在 attoparsec 中编写自己的解析器,因为它支持解析 ByteStrings。
如何使用attoparsec解析嵌套列表?
【问题讨论】:
-
你想一口气读完整个列表,还是分块处理?
-
一口气读完清单。
标签: parsing haskell bytestring attoparsec