Haskell:如何使用attoparsec从ByteString读取嵌套列表 [英] Haskell: How to use attoparsec in order to read a nested list from a ByteString
问题描述
[[p>]我有一个带有嵌套列表的文本文件(〜300 MB大) 4,9,11,28,30,45,55,58,61,62,63,69,74,76,77,82,87,92,93,94,95],[ 28,30,45,55,58,61,62,63,69,74,76,77,82,87,92,93,94],[4,9,11, 58,61,62,63,69,74,76,77,82,85,87,92,93,94,95]]
这是我的程序,将文件读入haskell Integer
列表:
< pre $
导入限定的Data.ByteString作为ByteStr
main :: IO()
- 如何做同样的事情,但使用用于文件访问的ByteStr.readFile?
main = do fContents< - readFile filePath
let numList = readNums fContents
putStrLn(show nums)
这适用于小文本文件,但我想用 ByteString
快速读取文件。我发现ByteString没有 read
函数,而应该在attoparsec中编写自己的解析器,因为它支持解析ByteStrings。
如何使用 attoparsec
解析嵌套列表?
Data.Aeson
decode
函数在 ByteString
上运行 $ b
导入限定的Data.ByteString.Lazy作为BL
import Data.Aeson
import Data.Maybe
main = do fContents< - BL.readFile filePath
let numList = decode fContents :: Maybe [[Int] ]
putStrLn(show $ fromJust numList)
I have a text file (~ 300 MB large) with a nested list, similar to this one:
[[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]]
Here is my program to read the file into a haskell Integer
list:
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)
This works for small text files, but I want to use ByteString
to read the file quickly. I found out that there is no read
function for ByteString, instead you should write your own parser in attoparsec, since it supports parsing ByteStrings.
How can I use attoparsec
to parse the nested list?
The data seems to be in JSON format, so you can use Data.Aeson
decode
function which works on 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)
这篇关于Haskell:如何使用attoparsec从ByteString读取嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!